Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust
227 KB
Raw Blame
// large_futures: stack futures on hot paths by design. too_many_lines: style gate
// only. Casts: 64-bit targets; narrowing sits at bounded OS/protocol boundaries.
#![allow(
    clippy::large_futures,
    clippy::too_many_lines,
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss
)]

//! End-to-end verifier for the interactive TUI: spawn the real supervisor
//! (`prime-agent --mode daemon`), drive the TUI headlessly against a scripted
//! daemon session, and assert on rendered frames plus daemon-side state. The
//! `script` seam is the faux provider contract; the product never sets it.
#![cfg(unix)]

use std::fmt::Write as _;
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};

use pa_types::daemon::DaemonCommand;

/// A one-pixel PNG (the clipboard seam fixture image).
const MINIMAL_PNG: &[u8] = &[
    137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0,
    0, 0, 31, 21, 196, 137, 0, 0, 0, 13, 73, 68, 65, 84, 120, 156, 99, 248, 207, 192, 240, 31, 0,
    5, 0, 1, 255, 137, 153, 61, 29, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130,
];

struct Supervisor {
    child: Child,
    socket: PathBuf,
}

/// Stop the daemon on `socket` by protocol so it can shut its workers down;
/// kill the child when the protocol path fails.
impl Drop for Supervisor {
    fn drop(&mut self) {
        graceful_shutdown(&self.socket);
        // Snapshot the live worker children before the kill: workers are detached,
        // so a timed-out graceful shutdown orphans them when the supervisor dies.
        let worker_pids = child_pids_of(self.child.id());
        let _ = self.child.kill();
        let _ = self.child.wait();
        for pid in worker_pids {
            kill_worker(pid);
        }
        let _ = std::fs::remove_file(&self.socket);
    }
}

/// Kill a leaked worker process (SIGKILL) and wait briefly for it to disappear.
fn kill_worker(pid: u32) {
    // The worker pid is not our child, so poll /proc liveness. Best effort by
    // design: an assert in `Drop` would abort the process and orphan the parallel
    // tests; the contract lives in `assert_daemon_stops_clean`.
    for round in 0..2 {
        unsafe {
            libc::kill(pid as i32, libc::SIGKILL);
        }
        let deadline = Instant::now() + Duration::from_secs(5);
        while process_alive(pid) {
            if Instant::now() >= deadline {
                break;
            }
            std::thread::sleep(Duration::from_millis(20));
        }
        if !process_alive(pid) {
            return;
        }
        eprintln!("worker {pid} survived teardown kill round {round}; re-killing");
    }
    eprintln!("worker {pid} still alive after two teardown kills");
}

/// RAII guard for a detached supervisor: shuts the daemon down on scope exit.
struct DetachedDaemon {
    socket: PathBuf,
}

impl Drop for DetachedDaemon {
    fn drop(&mut self) {
        // Best effort by design: this runs inside `Drop`, where an assert would
        // ABORT the process and orphan every other parallel test's daemons.
        let supervisor_pid = graceful_shutdown(&self.socket);
        if let Some(pid) = supervisor_pid {
            // Snapshot the supervisor's live worker children before it goes
            // (they are detached, so they survive its death).
            let worker_pids = child_pids_of(pid);
            let deadline = Instant::now() + Duration::from_secs(10);
            while process_alive(pid) {
                if Instant::now() >= deadline {
                    eprintln!("spawned supervisor {pid} missed the shutdown deadline; killing");
                    unsafe {
                        libc::kill(pid as i32, libc::SIGKILL);
                    }
                    break;
                }
                std::thread::sleep(Duration::from_millis(20));
            }
            for worker in worker_pids {
                kill_worker(worker);
            }
        }
        let _ = std::fs::remove_file(&self.socket);
    }
}

fn child_pids_of(ppid: u32) -> Vec<u32> {
    let mut pids = Vec::new();
    let entries = std::fs::read_dir("/proc").expect("read /proc");
    for entry in entries.flatten() {
        let Ok(entry_pid) = entry.file_name().to_string_lossy().parse::<u32>() else {
            continue;
        };
        let Ok(stat) = std::fs::read_to_string(format!("/proc/{entry_pid}/stat")) else {
            continue;
        };
        // `comm` can contain spaces and parens, so parse after the last ')'.
        let Some((_, rest)) = stat.rsplit_once(')') else {
            continue;
        };
        let mut fields = rest.split_whitespace();
        fields.next(); // process state
        let Ok(parent) = fields.next().unwrap_or_default().parse::<u32>() else {
            continue;
        };
        if parent == ppid {
            pids.push(entry_pid);
        }
    }
    pids
}

/// Liveness that ignores zombies: a detached child nobody reaps keeps its
/// `/proc` entry, so path existence alone would call an exited process alive.
fn process_alive(pid: u32) -> bool {
    let Ok(stat) = std::fs::read_to_string(format!("/proc/{pid}/stat")) else {
        return false;
    };
    // `comm` can contain spaces and parens, so parse after the last ')'.
    let Some((_, rest)) = stat.rsplit_once(')') else {
        return false;
    };
    let state = rest.split_whitespace().next().unwrap_or_default();
    !state.starts_with('Z') && !state.starts_with('X')
}

/// Shut the supervisor down by protocol and assert it, its workers, and its
/// socket all went away.
fn assert_daemon_stops_clean(socket: &Path) {
    // Sync JSONL exchange (called from the sync guard path).
    use std::io::{BufRead, BufReader, Write};
    use std::os::unix::net::UnixStream;

    let stream = UnixStream::connect(socket).expect("connect the spawned daemon");
    let write_half = stream.try_clone().expect("clone socket");
    let mut reader = BufReader::new(stream);
    let mut writer = write_half;
    let mut hello = String::new();
    reader.read_line(&mut hello).expect("read daemon_hello");
    let hello: serde_json::Value = serde_json::from_str(hello.trim()).expect("parse hello");
    let supervisor_pid = hello["supervisorPid"].as_u64().expect("supervisorPid") as u32;
    // The worker processes the supervisor spawned for live sessions, captured
    // before the shutdown so reparented workers can still be tracked.
    let worker_pids = child_pids_of(supervisor_pid);

    let command = serde_json::json!({
        "type": "command",
        "id": "stop-assert",
        "protocol": { "name": "prime-agent.daemon", "version": 7 },
        "command": { "type": "shutdown" },
    });
    let mut line = serde_json::to_string(&command).expect("serialize");
    line.push('\n');
    writer.write_all(line.as_bytes()).expect("send shutdown");
    writer.flush().expect("flush");

    let deadline = Instant::now() + Duration::from_secs(10);
    while process_alive(supervisor_pid) {
        assert!(
            Instant::now() < deadline,
            "the spawned supervisor {supervisor_pid} did not exit after shutdown"
        );
        std::thread::sleep(Duration::from_millis(50));
    }
    assert!(
        !socket.exists(),
        "the spawned supervisor removed its socket file"
    );
    for pid in worker_pids {
        let deadline = Instant::now() + Duration::from_secs(10);
        while process_alive(pid) {
            assert!(
                Instant::now() < deadline,
                "worker {pid} leaked after shutdown"
            );
            std::thread::sleep(Duration::from_millis(50));
        }
    }
}

/// Sync JSONL shutdown request (Drop runs inside the async test runtime, so
/// no nested runtime). Returns the supervisor pid from the hello.
fn graceful_shutdown(socket: &Path) -> Option<u32> {
    use std::io::{BufRead, BufReader, Write};
    use std::os::unix::net::UnixStream;

    let Ok(stream) = UnixStream::connect(socket) else {
        return None;
    };
    let Ok(write_half) = stream.try_clone() else {
        return None;
    };
    let mut reader = BufReader::new(stream);
    let mut writer = write_half;
    let mut hello = String::new();
    let _ = reader.read_line(&mut hello); // daemon_hello
    let supervisor_pid = serde_json::from_str::<serde_json::Value>(hello.trim())
        .ok()
        .and_then(|hello| hello["supervisorPid"].as_u64())
        .map(|pid| pid as u32);

    let command = serde_json::json!({
        "type": "command",
        "id": "test-shutdown",
        "protocol": { "name": "prime-agent.daemon", "version": 7 },
        "command": { "type": "shutdown" },
    });
    let Ok(mut line) = serde_json::to_string(&command) else {
        return None;
    };
    line.push('\n');
    if writer.write_all(line.as_bytes()).is_err() {
        return None;
    }
    let _ = writer.flush();
    // The response is the sync point: the supervisor stops every worker before exiting.
    let _ = reader
        .get_ref()
        .set_read_timeout(Some(Duration::from_secs(5)));
    let mut response = String::new();
    let _ = reader.read_line(&mut response);
    supervisor_pid
}

/// The headless run's wall: a turn that never settles holds the idle gate
/// closed and parks the run forever; the wall makes that a failing test.
const HEADLESS_RUN_BOUND: Duration = Duration::from_secs(300);

async fn run_headless_bounded(
    options: pa_tui::interactive::InteractiveOptions,
    plan: pa_tui::interactive::HeadlessPlan,
) -> anyhow::Result<pa_tui::interactive::InteractiveOutcome> {
    let started = Instant::now();
    match tokio::time::timeout(
        HEADLESS_RUN_BOUND,
        pa_tui::interactive::run_interactive(options, pa_tui::interactive::UiMode::Headless(plan)),
    )
    .await
    {
        Ok(outcome) => outcome,
        Err(tokio::time::error::Elapsed { .. }) => panic!(
            "the headless run exceeded the {HEADLESS_RUN_BOUND:?} wall after {:?}: the wedge class - a turn never settled and the idle gate never opened",
            started.elapsed()
        ),
    }
}

/// Kill supervisors leaked by earlier runs (a dead binary runs no `Drop`, so
/// its daemons are reparented to init): a daemon matches when its socket dir
/// is a `tempfile`-created `.tmpXXXXXX` dir and its parent is dead.
fn sweep_orphan_test_daemons() {
    static SWEEPED: std::sync::OnceLock<()> = std::sync::OnceLock::new();
    if SWEEPED.set(()).is_err() {
        return; // a later test's spawn: the first spawn already swept
    }
    let mut swept = 0;
    for entry in std::fs::read_dir("/proc").expect("read /proc").flatten() {
        let Ok(pid) = entry.file_name().to_string_lossy().parse::<u32>() else {
            continue;
        };
        let Ok(cmdline) = std::fs::read(format!("/proc/{pid}/cmdline")) else {
            continue;
        };
        // /proc cmdline is NUL-separated.
        let args: Vec<String> = cmdline
            .split(|b| *b == 0)
            .map(|b| String::from_utf8_lossy(b).into_owned())
            .collect();
        let flag = |needle: &str| args.iter().any(|a| a == needle);
        let arg_after = |needle: &str| {
            args.iter()
                .position(|a| a == needle)
                .and_then(|idx| args.get(idx + 1))
                .cloned()
        };
        if !flag("--mode") || arg_after("--mode").as_deref() != Some("daemon") {
            continue;
        }
        let Some(socket) = arg_after("--daemon-socket") else {
            continue;
        };
        // A live run's daemon keeps its owning test binary as the parent, and
        // the user's product daemons fail the socket-dir test, so neither is swept.
        let orphan_socket_dir = Path::new(&socket)
            .parent()
            .and_then(Path::file_name)
            .and_then(|name| name.to_str())
            .is_some_and(|name| name.starts_with(".tmp"));
        if !orphan_socket_dir {
            continue;
        }
        let Ok(stat) = std::fs::read_to_string(format!("/proc/{pid}/stat")) else {
            continue;
        };
        let Some((_, rest)) = stat.rsplit_once(')') else {
            continue;
        };
        let Ok(parsed_ppid) = rest
            .split_whitespace()
            .nth(1)
            .unwrap_or_default()
            .parse::<u32>()
        else {
            continue;
        };
        if parsed_ppid != 1 && process_alive(parsed_ppid) {
            continue; // a live run's daemon: its test binary is still up
        }
        unsafe {
            libc::kill(pid as i32, libc::SIGKILL);
        }
        swept += 1;
    }
    if swept > 0 {
        eprintln!("swept {swept} orphan test daemon(s) (socket dir gone) before spawning");
    }
}

#[allow(clippy::zombie_processes)]
fn spawn_supervisor(dir: &Path) -> Supervisor {
    sweep_orphan_test_daemons();
    let socket = dir.join("daemon.sock");
    let agent_dir = dir.join("agent");
    std::fs::create_dir_all(&agent_dir).expect("agent dir");
    let mut command = Command::new(env!("CARGO_BIN_EXE_prime-agent"));
    command
        .args(["--mode", "daemon", "--daemon-socket"])
        .arg(&socket)
        .env("PRIME_AGENT_CODING_AGENT_DIR", &agent_dir)
        // The daemon's startup catalog refresh must never reach the network
        // from a test: PI_OFFLINE keeps it on the bundled/models.json snapshot.
        .env("PI_OFFLINE", "1")
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null());
    // The launcher strips inherited worker role env vars before spawning the
    // supervisor; a CLI running inside a daemon worker must not leak them.
    for var in [
        pa_daemon::worker::WORKER_ROLE_ENV,
        pa_daemon::worker::WORKER_TOKEN_ENV,
        pa_daemon::worker::WORKER_ACTIVE_SESSION_ID_ENV,
        pa_daemon::worker::WORKER_RECOVERY_JOURNAL_ENV,
        pa_daemon::worker::WORKER_SUPERVISOR_SOCKET_ENV,
        pa_daemon::worker::WORKER_SOCKET_ENV,
        pa_daemon::worker::WORKER_INSTANCE_ID_ENV,
        pa_daemon::worker::WORKER_SCRIPT_ENV,
    ] {
        command.env_remove(var);
    }
    // Ambient provider credentials must not leak into the daemon's catalog:
    // every spawned supervisor here serves models.json fixtures only.
    for provider in pa_ai::models_generated::get_providers() {
        if let Some(vars) = pa_ai::env_api_keys::get_api_key_env_vars(provider) {
            for var in vars {
                command.env_remove(var);
            }
        }
    }
    command.env_remove("PRIME_TEAM_ID");
    // A supervisor killed by a failing test must not leak its session workers:
    // the supervisor-lost exit runs on this short window, not the 5-minute default.
    command.env(
        pa_daemon::worker::WORKER_SUPERVISOR_LOST_EXIT_MS_ENV,
        "15000",
    );
    // A full workspace run can starve a session worker's boot far past the
    // 30s default connect budget; this keeps session creates deterministic.
    command.env("PA_DAEMON_WORKER_CONNECT_TIMEOUT_MS", "90000");
    // Die with this test binary: the kernel SIGKILLs a supervisor that gets
    // reparented to init; the guard's protocol teardown stays the exit path.
    unsafe {
        command.pre_exec(move || {
            libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL);
            Ok(())
        });
    }
    let child = command.spawn().expect("spawn prime-agent --mode daemon");
    let deadline = Instant::now() + Duration::from_mins(1);
    while Instant::now() < deadline {
        if socket.exists() {
            return Supervisor { child, socket };
        }
        std::thread::sleep(Duration::from_millis(20));
    }
    panic!("supervisor socket never appeared");
}

/// Create a live session through the daemon protocol, writing the scripted engine config first.
async fn create_session_via_daemon(
    socket: &Path,
    script_path: &Path,
    script: &serde_json::Value,
    cwd: &Path,
    session_dir: &Path,
) -> String {
    std::fs::write(script_path, script.to_string()).expect("write script");
    let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(socket)
        .await
        .expect("connect supervisor");
    let data = client
        .request_ok(DaemonCommand::Create {
            id: None,
            session_path: None,
            continue_recent: None,
            no_session: None,
            name: None,
            config: Some(serde_json::json!({
                "cwd": cwd.display().to_string(),
                "sessionDir": session_dir.display().to_string(),
                "script": script_path.display().to_string(),
            })),
            telemetry_disabled: None,
            runtime_metadata: None,
            lifecycle: None,
            env: None,
            launch_env: None,
            rest: serde_json::Map::default(),
        })
        .await
        .expect("create session");
    client.close();
    data.get("activeSessionId")
        .or_else(|| data.get("id"))
        .and_then(serde_json::Value::as_str)
        .expect("session id")
        .to_string()
}

#[tokio::test]
async fn tui_attaches_prompts_streams_lists_and_switches() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    // A second live session via the daemon protocol, so the switch target is known by id.
    let script = serde_json::json!({ "responses": [
        { "text": "hello from scripted", "delayMs": 20 },
        { "text": "second turn" },
    ] });
    let second = create_session_via_daemon(
        &supervisor.socket,
        &dir.path().join("script.json"),
        &script,
        dir.path(),
        &session_dir,
    )
    .await;

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(dir.path().join("script.json")),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),

        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("hi".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            pa_tui::interactive::HeadlessStep::Submit("again".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            // Session list, then switch to the second session by id.
            pa_tui::interactive::HeadlessStep::Submit("/list".to_string()),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "live sessions:".to_string(),
                timeout_ms: 30_000,
            },
            pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
                crossterm::event::KeyCode::Esc,
                crossterm::event::KeyModifiers::NONE,
            )),
            pa_tui::interactive::HeadlessStep::Submit(format!("/switch {second}")),
            pa_tui::interactive::HeadlessStep::Submit("third".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");

    assert!(!outcome.frames.is_empty(), "frames were captured");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("hello from scripted"),
        "first scripted turn rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("second turn"),
        "second scripted turn rendered:\n{rendered}"
    );
    assert!(rendered.contains("hi"), "user message echoed:\n{rendered}");
    assert!(
        rendered.contains("again"),
        "queued prompt rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("live sessions:"),
        "session list rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("switched to session"),
        "switch note rendered:\n{rendered}"
    );
    assert_eq!(
        outcome.active_session_id, second,
        "the run ended attached to the switched session"
    );
    assert_eq!(
        outcome.last_assistant_text.as_deref(),
        Some("hello from scripted"),
        "the switched session produced its first scripted turn"
    );

    let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(&supervisor.socket)
        .await
        .expect("connect supervisor");
    let last = client
        .request_ok(DaemonCommand::GetLastAssistantText {
            id: None,
            active_session_id: second.clone(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_last_assistant_text");
    assert_eq!(last["text"], "hello from scripted");
    let sessions = client
        .request_ok(DaemonCommand::List {
            id: None,
            all: None,
            cwd: None,
            session_dir: None,
            include_client_owned: None,
            rest: serde_json::Map::default(),
        })
        .await
        .expect("list");
    assert_eq!(
        sessions["sessions"].as_array().map(Vec::len),
        Some(2),
        "both sessions stay live after the TUI exited: {sessions}"
    );
    client.close();

    let persisted = std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
        .filter(|entry| entry.path().extension().and_then(|e| e.to_str()) == Some("jsonl"))
        .count();
    assert_eq!(persisted, 2, "two session files persisted");
    drop(supervisor);
}

/// The product's settings-backed onboarding persistence (`SettingsOnboardingSink` glue).
struct FreshHomeOnboardingSink {
    cwd: PathBuf,
    agent_dir: PathBuf,
}

impl pa_tui::interactive::OnboardingSink for FreshHomeOnboardingSink {
    fn onboarding_shown(&self) -> bool {
        pa_core::settings::SettingsManager::create(&self.cwd, &self.agent_dir)
            .get_onboarding_shown()
    }

    fn agent_traces_choice_written(&self) -> bool {
        pa_core::settings::SettingsManager::create(&self.cwd, &self.agent_dir)
            .agent_traces_choice_written()
    }

    fn set_agent_traces_enabled(&self, enabled: bool) -> anyhow::Result<()> {
        pa_core::settings::SettingsManager::create(&self.cwd, &self.agent_dir)
            .set_agent_traces_enabled(enabled)
    }

    fn mark_onboarding_complete(&self) -> anyhow::Result<()> {
        pa_core::settings::SettingsManager::create(&self.cwd, &self.agent_dir)
            .set_onboarding_shown(true)
    }

    fn onboarding_incomplete(
        &self,
        _outcome: &'static str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + '_>> {
        Box::pin(std::future::ready(()))
    }
}

/// The product sink whose completion write always fails: reads stay real, the write errors.
struct FailingMarkOnboardingSink {
    cwd: PathBuf,
    agent_dir: PathBuf,
}

impl pa_tui::interactive::OnboardingSink for FailingMarkOnboardingSink {
    fn onboarding_shown(&self) -> bool {
        pa_core::settings::SettingsManager::create(&self.cwd, &self.agent_dir)
            .get_onboarding_shown()
    }

    fn agent_traces_choice_written(&self) -> bool {
        pa_core::settings::SettingsManager::create(&self.cwd, &self.agent_dir)
            .agent_traces_choice_written()
    }

    fn set_agent_traces_enabled(&self, _enabled: bool) -> anyhow::Result<()> {
        Ok(())
    }

    fn mark_onboarding_complete(&self) -> anyhow::Result<()> {
        Err(anyhow::anyhow!("settings disk full"))
    }

    fn onboarding_incomplete(
        &self,
        _outcome: &'static str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + '_>> {
        Box::pin(std::future::ready(()))
    }
}

/// A fresh install asks the trace question exactly once; TS asks unconditionally (divergence).
#[tokio::test]
async fn fresh_home_asks_the_trace_question_once_and_completes() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    let script = serde_json::json!({ "responses": [
        { "text": "hello fresh home", "delayMs": 20 },
    ] });
    std::fs::write(
        dir.path().join("script.json"),
        serde_json::to_string(&script).expect("script json"),
    )
    .expect("script.json");

    // The task mounts exactly as the product's model-ready gate builds it.
    let mut options = base_options(&supervisor, dir.path(), &session_dir);
    options.onboarding = Some(pa_tui::interactive::OnboardingTask {
        sink: std::sync::Arc::new(FreshHomeOnboardingSink {
            cwd: dir.path().to_path_buf(),
            agent_dir: agent_dir.clone(),
        }),
        model_ready: std::sync::Arc::new(|| true),
        current_model: None,
        provider_auth: None,
    });
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
                crossterm::event::KeyCode::Enter,
                crossterm::event::KeyModifiers::NONE,
            )),
            pa_tui::interactive::HeadlessStep::Submit("hi".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");

    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("Share agent traces"),
        "the onboarding question rendered once for the fresh home:\n{rendered}"
    );
    assert!(
        rendered.contains("hello fresh home"),
        "the answered dialog released the pane and the first turn ran:\n{rendered}"
    );
    let settings = pa_core::settings::SettingsManager::create(dir.path(), &agent_dir);
    assert!(
        settings.get_onboarding_shown(),
        "the answered flow marked onboarding shown"
    );
    assert!(
        settings.get_agent_traces_enabled(),
        "the pre-selected Share answer persisted"
    );
    drop(supervisor);
}

/// A provisioned home (sharing explicitly opted out) never sees the question
/// (TS #2368 asks it; the operator's existing-user ruling removes it here).
#[tokio::test]
async fn provisioned_opt_out_home_completes_silently_without_the_question() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    std::fs::write(
        agent_dir.join("settings.json"),
        r#"{ "agentTraces": { "enabled": false } }"#,
    )
    .expect("provisioned settings");
    let supervisor = spawn_supervisor(dir.path());

    let script = serde_json::json!({ "responses": [
        { "text": "hello provisioned home", "delayMs": 20 },
    ] });
    std::fs::write(
        dir.path().join("script.json"),
        serde_json::to_string(&script).expect("script json"),
    )
    .expect("script.json");

    let mut options = base_options(&supervisor, dir.path(), &session_dir);
    options.onboarding = Some(pa_tui::interactive::OnboardingTask {
        sink: std::sync::Arc::new(FreshHomeOnboardingSink {
            cwd: dir.path().to_path_buf(),
            agent_dir: agent_dir.clone(),
        }),
        model_ready: std::sync::Arc::new(|| true),
        current_model: None,
        provider_auth: None,
    });
    // No key step answers anything: the flow must complete before the submission runs.
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("hi".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");

    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("hello provisioned home"),
        "the session started directly and completed its first turn:\n{rendered}"
    );
    assert!(
        !rendered.contains("Share agent traces"),
        "the question never owned a session frame:\n{rendered}"
    );

    let settings = pa_core::settings::SettingsManager::create(dir.path(), &agent_dir);
    assert!(
        !settings.get_agent_traces_enabled(),
        "the standing opt-out survived the silent completion"
    );
    assert!(
        settings.get_onboarding_shown(),
        "the silent flow marked onboarding shown"
    );
    drop(supervisor);
}

/// The scripted provider-auth surface the full-flow verifier drives: the
/// Prime Inference row, one api-key provider row, one `mcp:` service row.
struct FullFlowProviderAuth {
    agent_dir: PathBuf,
}

impl FullFlowProviderAuth {
    fn stored(&self, provider: &str) -> bool {
        pa_core::auth::AuthStorage::create(&self.agent_dir)
            .get_all()
            .credential(provider)
            .is_some()
    }
}

impl pa_tui::provider_auth::ProviderAuthCommands for FullFlowProviderAuth {
    fn login_options(&self) -> pa_tui::provider_auth::ProviderRowsFuture {
        let rows = vec![
            pa_tui::provider_auth::ProviderRow {
                id: pa_tui::provider_auth::PRIME_INFERENCE_PROVIDER_ID.to_string(),
                name: "Prime Inference".to_string(),
                auth_type: pa_tui::provider_auth::AuthType::ApiKey,
                status: Some(pa_tui::provider_auth::AuthStatusIndicator {
                    style: pa_tui::provider_auth::AuthStatusStyle::Success,
                    label: "configured".to_string(),
                }),
                flow: pa_tui::provider_auth::AuthFlow::TerminalFlow,
                configured: self.stored(pa_tui::provider_auth::PRIME_INFERENCE_PROVIDER_ID),
                available: true,
            },
            pa_tui::provider_auth::ProviderRow {
                id: "faux-key".to_string(),
                name: "Faux Key".to_string(),
                auth_type: pa_tui::provider_auth::AuthType::ApiKey,
                status: None,
                flow: pa_tui::provider_auth::AuthFlow::ApiKeyPrompt,
                configured: self.stored("faux-key"),
                available: true,
            },
            pa_tui::provider_auth::ProviderRow {
                id: "mcp:faux".to_string(),
                name: "Faux MCP".to_string(),
                auth_type: pa_tui::provider_auth::AuthType::Oauth,
                status: None,
                flow: pa_tui::provider_auth::AuthFlow::TerminalFlow,
                configured: false,
                available: true,
            },
        ];
        Box::pin(async move { rows })
    }

    fn logout_options(&self) -> pa_tui::provider_auth::ProviderRowsFuture {
        Box::pin(async move { Vec::new() })
    }

    fn login(
        &self,
        provider: &pa_tui::provider_auth::ProviderRow,
        api_key: Option<&str>,
    ) -> pa_tui::provider_auth::ProviderAuthFuture {
        let agent_dir = self.agent_dir.clone();
        let provider_id = provider.id.clone();
        let provider_name = provider.name.clone();
        let key = api_key.map(str::to_string);
        Box::pin(async move {
            let mut auth = pa_core::auth::AuthStorage::create(&agent_dir);
            auth.set(
                &provider_id,
                pa_core::auth::AuthCredential::ApiKey {
                    key: key.unwrap_or_default(),
                    prime_team: None,
                },
            );
            if auth.drain_errors().pop().is_some() {
                return pa_tui::provider_auth::ProviderAuthOutcome::Error(format!(
                    "Failed to save API key for {provider_name}"
                ));
            }
            pa_tui::provider_auth::ProviderAuthOutcome::Status(format!(
                "Saved API key for {provider_name}"
            ))
        })
    }

    fn login_on_panel(
        &self,
        provider: &pa_tui::provider_auth::ProviderRow,
        panel: pa_tui::auth_panel::AuthPanelHandle,
    ) -> pa_tui::provider_auth::ProviderAuthFuture {
        let agent_dir = self.agent_dir.clone();
        let provider_id = provider.id.clone();
        let provider_name = provider.name.clone();
        Box::pin(async move {
            // Only the Prime row runs here; anything else cancels.
            if provider_id != pa_tui::provider_auth::PRIME_INFERENCE_PROVIDER_ID {
                return pa_tui::provider_auth::ProviderAuthOutcome::Cancelled;
            }
            panel.progress("Checking Prime Inference access...");
            let Some(api_key) = panel
                .paste_prompt(
                    "Paste a Prime API key below:",
                    pa_tui::auth_panel::PastePromptTone::Muted,
                    pa_tui::auth_panel::PasteStyle::Visible,
                )
                .await
            else {
                return pa_tui::provider_auth::ProviderAuthOutcome::Cancelled;
            };
            let mut auth = pa_core::auth::AuthStorage::create(&agent_dir);
            auth.set(
                &provider_id,
                pa_core::auth::AuthCredential::ApiKey {
                    key: api_key,
                    prime_team: None,
                },
            );
            if auth.drain_errors().pop().is_some() {
                return pa_tui::provider_auth::ProviderAuthOutcome::Error(format!(
                    "Failed to login to {provider_name}"
                ));
            }
            pa_tui::provider_auth::ProviderAuthOutcome::Status(format!(
                "Saved API key for {provider_name}. Credentials saved to {}.",
                agent_dir.join("auth.json").display()
            ))
        })
    }

    fn logout(
        &self,
        _provider: &pa_tui::provider_auth::ProviderRow,
    ) -> pa_tui::provider_auth::ProviderAuthFuture {
        Box::pin(async move { pa_tui::provider_auth::ProviderAuthOutcome::Cancelled })
    }

    fn anthropic_subscription_warning(&self) -> pa_tui::provider_auth::ProviderWarningFuture {
        // The faux full-flow drives no Anthropic subscription auth.
        Box::pin(async move { None })
    }
}

/// The full first-run flow with no usable model (TS `runOnboardingFlow`'s
/// not-ready branch): every answer, both credentials, and the flag persist.
#[tokio::test]
async fn fresh_home_runs_the_full_sign_in_flow_to_completion() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    let script = serde_json::json!({ "responses": [
        { "text": "hello full flow", "delayMs": 20 },
    ] });
    std::fs::write(
        dir.path().join("script.json"),
        serde_json::to_string(&script).expect("script json"),
    )
    .expect("script.json");

    let mut options = base_options(&supervisor, dir.path(), &session_dir);
    // The readiness probe mirrors the flow's contract: not ready until the
    // sign-in stores its credential (the model-ready gate at flow end).
    let probe_agent_dir = agent_dir.clone();
    let model_ready = std::sync::Arc::new(move || {
        pa_core::auth::AuthStorage::create(&probe_agent_dir)
            .get_all()
            .credential(pa_tui::provider_auth::PRIME_INFERENCE_PROVIDER_ID)
            .is_some()
    });
    options.onboarding = Some(pa_tui::interactive::OnboardingTask {
        sink: std::sync::Arc::new(FreshHomeOnboardingSink {
            cwd: dir.path().to_path_buf(),
            agent_dir: agent_dir.clone(),
        }),
        model_ready,
        current_model: None,
        provider_auth: Some(pa_tui::provider_auth::ProviderAuthCommandsHandle(
            std::sync::Arc::new(FullFlowProviderAuth {
                agent_dir: agent_dir.clone(),
            }),
        )),
    });
    let enter = || {
        pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
            crossterm::event::KeyCode::Enter,
            crossterm::event::KeyModifiers::NONE,
        ))
    };
    let down = || {
        pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
            crossterm::event::KeyCode::Down,
            crossterm::event::KeyModifiers::NONE,
        ))
    };
    // The plan waits on observable readiness, not fixed sleeps: each barrier
    // holds the queued batch until a rendered frame contains the condition.
    let wait_render = |needle: &str| pa_tui::interactive::HeadlessStep::WaitRender {
        needle: needle.to_string(),
        timeout_ms: 5_000,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            enter(),
            wait_render("Paste a Prime API key below:"),
            pa_tui::interactive::HeadlessStep::Type("faux-prime-key".to_string()),
            enter(),
            wait_render("Connect other providers, or continue."),
            down(),
            enter(),
            wait_render("Enter API key"),
            pa_tui::interactive::HeadlessStep::Type("faux-key".to_string()),
            enter(),
            wait_render("\u{2713}"),
            enter(),
            wait_render("Share agent traces"),
            enter(),
            pa_tui::interactive::HeadlessStep::Submit("hi".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");

    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("Log in with Prime Intellect"),
        "the welcome screen's action rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Login with Prime Intellect"),
        "the login dialog's heading replaced the brand line:\n{rendered}"
    );
    assert!(
        rendered.contains("Paste a Prime API key below:"),
        "the paste prompt mounted inside the pane:\n{rendered}"
    );
    assert!(
        rendered.contains("Connect other providers, or continue."),
        "the providers picker rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Share agent traces"),
        "the trace question ended the flow:\n{rendered}"
    );
    assert!(
        rendered.contains("hello full flow"),
        "the completed flow released the pane and the first turn ran:\n{rendered}"
    );
    // The scripted engine refuses live model switches by design, so the
    // refusal row is the proof the apply REQUEST reached it.
    assert!(
        rendered.contains("This session does not support model switching"),
        "the apply round-tripped and the scripted engine's refusal surfaced:\n{rendered}"
    );
    assert!(
        rendered.contains("Saved API key for Faux Key"),
        "the provider login's status row applied:\n{rendered}"
    );

    let settings = pa_core::settings::SettingsManager::create(dir.path(), &agent_dir);
    assert!(
        settings.get_onboarding_shown(),
        "the completed flow marked onboarding shown"
    );
    assert!(
        settings.get_agent_traces_enabled(),
        "the Share answer persisted"
    );
    let auth = pa_core::auth::AuthStorage::create(&agent_dir);
    assert!(
        auth.get_all()
            .credential(pa_tui::provider_auth::PRIME_INFERENCE_PROVIDER_ID)
            .is_some(),
        "the Prime sign-in stored its credential"
    );
    assert!(
        auth.get_all().credential("faux-key").is_some(),
        "the provider login stored its key"
    );
    drop(supervisor);
}

#[tokio::test]
async fn a_failed_completion_write_surfaces_a_warning_and_never_kills_the_run() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    std::fs::write(
        agent_dir.join("settings.json"),
        r#"{ "agentTraces": { "enabled": false } }"#,
    )
    .expect("provisioned settings");
    let supervisor = spawn_supervisor(dir.path());

    let script = serde_json::json!({ "responses": [
        { "text": "hello despite the write failure", "delayMs": 20 },
    ] });
    std::fs::write(
        dir.path().join("script.json"),
        serde_json::to_string(&script).expect("script json"),
    )
    .expect("script.json");

    let mut options = base_options(&supervisor, dir.path(), &session_dir);
    options.onboarding = Some(pa_tui::interactive::OnboardingTask {
        sink: std::sync::Arc::new(FailingMarkOnboardingSink {
            cwd: dir.path().to_path_buf(),
            agent_dir: agent_dir.clone(),
        }),
        model_ready: std::sync::Arc::new(|| true),
        current_model: None,
        provider_auth: None,
    });
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("hi".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("the run survives the failed write");

    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("hello despite the write failure"),
        "the session ran its first turn despite the failed write:\n{rendered}"
    );
    assert!(
        !rendered.contains("Share agent traces"),
        "the standing choice never re-opened the question:\n{rendered}"
    );
    assert!(
        rendered.contains("could not be saved"),
        "the failed persistence surfaced as a warning row:\n{rendered}"
    );
    let settings = pa_core::settings::SettingsManager::create(dir.path(), &agent_dir);
    assert!(
        !settings.get_onboarding_shown(),
        "the failed write left the marker unset"
    );
    drop(supervisor);
}

/// The re-show regression: the agents-view flow re-runs the onboarding phase
/// per session open, so the phase gates on the persisted marker itself.
#[tokio::test]
async fn a_completed_flow_never_reopens_the_question_for_a_later_session() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    // The faux queue spans one session (each created session replays from the top).
    let script = serde_json::json!({ "responses": [
        { "text": "hello each session", "delayMs": 20 },
    ] });
    std::fs::write(
        dir.path().join("script.json"),
        serde_json::to_string(&script).expect("script json"),
    )
    .expect("script.json");

    // The SAME options object backs both session runs, as the agents-view flow clones `base`.
    let mut options = base_options(&supervisor, dir.path(), &session_dir);
    options.onboarding = Some(pa_tui::interactive::OnboardingTask {
        sink: std::sync::Arc::new(FreshHomeOnboardingSink {
            cwd: dir.path().to_path_buf(),
            agent_dir: agent_dir.clone(),
        }),
        model_ready: std::sync::Arc::new(|| true),
        current_model: None,
        provider_auth: None,
    });
    // Down + Enter answers `Not now` (the answer that used to re-show), then the turn runs.
    let first_plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
                crossterm::event::KeyCode::Down,
                crossterm::event::KeyModifiers::NONE,
            )),
            pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
                crossterm::event::KeyCode::Enter,
                crossterm::event::KeyModifiers::NONE,
            )),
            pa_tui::interactive::HeadlessStep::Submit("hi".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let first = run_headless_bounded(options.clone(), first_plan)
        .await
        .expect("first interactive run");
    let first_rendered = first.frames.join("\n");
    assert!(
        first_rendered.contains("Share agent traces"),
        "the fresh home was asked once:\n{first_rendered}"
    );
    assert!(
        first_rendered.contains("hello each session"),
        "the answered dialog released the pane and the first turn ran:\n{first_rendered}"
    );
    let settings = pa_core::settings::SettingsManager::create(dir.path(), &agent_dir);
    assert!(
        !settings.get_agent_traces_enabled(),
        "the Not-now answer persisted (the opt-out)"
    );
    assert!(
        settings.get_onboarding_shown(),
        "the completion flag persisted with the answer"
    );

    // The second session, same task: no key answers anything; the marker must gate itself.
    let second_plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("again".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let second = run_headless_bounded(options, second_plan)
        .await
        .expect("second interactive run");
    let second_rendered = second.frames.join("\n");
    assert!(
        second_rendered.contains("hello each session"),
        "the second session started directly and completed its turn:\n{second_rendered}"
    );
    assert!(
        !second_rendered.contains("Share agent traces"),
        "the completed flow never reopened the question:\n{second_rendered}"
    );
    drop(supervisor);
}

#[tokio::test]
async fn ensure_daemon_running_spawns_supervisor_and_tui_attaches() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let socket = dir.path().join("spawned.sock");
    std::env::set_var("PRIME_AGENT_CODING_AGENT_DIR", &agent_dir);
    // The internally-spawned supervisor inherits this process's env: the short
    // supervisor-lost window keeps a killed supervisor from leaking workers.
    std::env::set_var(
        pa_daemon::worker::WORKER_SUPERVISOR_LOST_EXIT_MS_ENV,
        "15000",
    );

    let script_path = dir.path().join("script.json");
    std::fs::write(
        &script_path,
        serde_json::json!({ "responses": [{ "text": "spawned hello" }] }).to_string(),
    )
    .expect("write script");

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: Some("boot".to_string()),
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),

        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    // The interactive runtime's launch sequence, minus the TTY: spawn detached, wait for hello.
    let _guard = DetachedDaemon {
        socket: socket.clone(),
    };
    let exe = std::path::PathBuf::from(env!("CARGO_BIN_EXE_prime-agent"));
    pa_cli::ensure_daemon_running_with(&exe, &socket, dir.path())
        .await
        .expect("spawn the daemon");
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 }],
        width: 80,
        height: 24,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("headless interactive run");
    assert!(
        outcome
            .frames
            .iter()
            .any(|frame| frame.contains("spawned hello")),
        "initial message ran against the spawned daemon:\n{}",
        outcome.frames.join("\n")
    );
    assert_eq!(
        outcome.last_assistant_text.as_deref(),
        Some("spawned hello")
    );

    assert_daemon_stops_clean(&socket);
}

#[tokio::test]
async fn tui_dispatches_slash_commands_menu_and_suggestions() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    // The faux engine drives the real agent engine over the scripted faux
    // provider, so the session-command admission path runs as in the product.
    let script = serde_json::json!({ "engine": "faux", "responses": [
        { "text": "scripted reply" },
    ] });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(dir.path().join("script.json")),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),

        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("/goal status".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            pa_tui::interactive::HeadlessStep::Submit("/modle".to_string()),
            // Probe pass over the autocomplete menu (typed keystroke by
            // keystroke, completed with Enter). The attach-time
            // slash-command catalog refresh is still in flight here: its
            // background `get_commands` response swaps the provider and
            // closes an open dropdown when it lands (TS
            // `setAutocompleteProvider` parity), so this pass may lose its
            // menu to the swap. Two cases keep the asserted passes below
            // behind the landing: with the menu still open, the
            // `skill:goal` row renders in a post-landing frame and the
            // barrier pops at the landing; a landing that closes the menu
            // without re-parking never renders the needle, and the 15s
            // bound — past the 10s fetch deadline — is what waits out
            // that case.
            pa_tui::interactive::HeadlessStep::Type("/".to_string()),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            pa_tui::interactive::HeadlessStep::Type("goa".to_string()),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "skill:goal".to_string(),
                timeout_ms: 15_000,
            },
            // With the dropdown open, Enter completes the selected
            // suggestion (`/goal `); the second Enter submits it.
            pa_tui::interactive::HeadlessStep::Type("\n".to_string()),
            pa_tui::interactive::HeadlessStep::Type("\n".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            // The menu assertions run against the landed catalog: the first
            // registry entry selected at `/`, `/goa` fuzzy-matching goal —
            // no in-flight swap can close these dropdowns between
            // materialize and render.
            pa_tui::interactive::HeadlessStep::Type("/".to_string()),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "\u{203a} settings".to_string(),
                timeout_ms: 30_000,
            },
            pa_tui::interactive::HeadlessStep::Type("goa".to_string()),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "\u{203a} goal".to_string(),
                timeout_ms: 30_000,
            },
            pa_tui::interactive::HeadlessStep::Type("\n".to_string()),
            pa_tui::interactive::HeadlessStep::Type("\n".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            // `/model` LAST: the inline menu-panel opens and owns the keys
            // from here on (TS `showConfigurationMenu`).
            pa_tui::interactive::HeadlessStep::Submit("/model".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 120,
        height: 36,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");

    if let Ok(dir) = std::env::var("PA_TUI_DUMP_FRAMES") {
        for (index, frame) in outcome.frames.iter().enumerate() {
            let _ = std::fs::write(
                std::path::Path::new(&dir).join(format!("frame-{index:03}.txt")),
                frame,
            );
        }
    }
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("/goal status"),
        "the session-command echo row rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("No active goal."),
        "the session-command result row rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Unknown command: /modle. Did you mean /model?"),
        "the unknown-command suggestion matched the TS string:\n{rendered}"
    );
    assert!(
        rendered.contains("Search models"),
        "the /model command opened the inline menu-panel:\n{rendered}"
    );
    assert!(
        rendered.contains("Enter select \u{b7} Esc close"),
        "the menu-panel hint rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("\u{203a} settings"),
        "the slash menu rendered with the selected first entry:\n{rendered}"
    );
    assert!(
        rendered.contains("Open settings menu"),
        "the selected item's description rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("\u{203a} goal"),
        "the fuzzy best match for /goa rendered selected:\n{rendered}"
    );

    let mut saw_echo = false;
    let mut saw_result = false;
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
            continue;
        }
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        saw_echo |= content.contains("\"session_slash_command\"");
        saw_result |= content.contains("\"session_slash_command_result\"");
    }
    assert!(
        saw_echo,
        "the session file persisted the session_slash_command rows"
    );
    assert!(
        saw_result,
        "the session file persisted the session_slash_command_result rows"
    );
    drop(supervisor);
}

/// `/model` + `/effort` through the daemon: a models.json custom model lists
/// and Enter applies it (durable `model_change` row); `/effort` without
/// reasoning reports the TS unsupported note (an `off`-only list is no thinking).
#[tokio::test]
async fn tui_model_picker_applies_and_effort_reports() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    // A models.json custom provider carries the model, so it resolves without any network.
    std::fs::write(
        agent_dir.join("models.json"),
        serde_json::json!({
            "providers": {
                "test-provider": {
                    "api": "openai-completions",
                    "baseUrl": "http://127.0.0.1:9/v1",
                    "apiKey": "sk-test",
                    "models": [
                        { "id": "mock-1", "name": "Mock 1", "api": "openai-completions",
                          "baseUrl": "http://127.0.0.1:9/v1", "contextWindow": 128_000,
                          "maxTokens": 4096 }
                    ]
                }
            }
        })
        .to_string(),
    )
    .expect("write models.json");
    let script = serde_json::json!({ "engine": "faux", "responses": [
        { "text": "scripted reply" },
    ] });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let supervisor = spawn_supervisor(dir.path());
    // The catalog snapshot the composition root injects, pinned hermetically:
    // ambient credentials (PRIME_API_KEY on the dev box) cannot leak in.
    let auth = pa_core::auth::AuthStorage::in_memory_without_env(
        &pa_core::auth::AuthStorageData::default(),
        std::sync::Arc::new(pa_core::auth::NoOAuth),
    );
    let mut registry = pa_core::models::ModelRegistry::create(auth, agent_dir.join("models.json"));
    registry.load_private_authorization_from_cache();
    let catalog: Vec<pa_types::ai::Model> = registry.get_available().into_iter().cloned().collect();
    assert_eq!(catalog.len(), 1, "the models.json model resolves available");
    assert_eq!(
        catalog[0].id, "mock-1",
        "the one available model is the models.json mock"
    );

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(dir.path().join("script.json")),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: catalog,
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),

        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("/model".to_string()),
            pa_tui::interactive::HeadlessStep::Type("mock".to_string()),
            pa_tui::interactive::HeadlessStep::Type("\n".to_string()),
            pa_tui::interactive::HeadlessStep::Submit("/effort".to_string()),
            // ctrl+l opens the picker over the user's own text: the pick must keep it.
            pa_tui::interactive::HeadlessStep::Type("keep me".to_string()),
            pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
                crossterm::event::KeyCode::Char('l'),
                crossterm::event::KeyModifiers::CONTROL,
            )),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Search models".to_string(),
                timeout_ms: 30_000,
            },
            pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
                crossterm::event::KeyCode::Enter,
                crossterm::event::KeyModifiers::NONE,
            )),
        ],
        width: 120,
        height: 36,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("Mock 1"),
        "the /model picker listed the models.json model by name:\n{rendered}"
    );
    assert!(
        rendered.contains("Model: mock-1"),
        "picking the model showed the TS confirm row:\n{rendered}"
    );
    assert!(
        rendered.contains("Current model does not support thinking"),
        "the /effort command reported the TS unsupported-model note:\n{rendered}"
    );

    // The durable rows persisted: the creation-prefix `model_change` plus the
    // switch's own row (a switch to the current model still records one).
    let mut model_changes = 0;
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
            continue;
        }
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        model_changes += content
            .lines()
            .filter(|line| line.contains(r#""type":"model_change""#))
            .count();
    }
    assert!(
        model_changes >= 2,
        "the set_model switch persisted its model_change row (saw {model_changes})"
    );
    // The ctrl+l pick kept the editor's own text (an apply-side clear would leave it empty).
    let last = outcome.frames.last().expect("a final frame");
    assert!(
        last.contains("keep me"),
        "the ctrl+l pick kept the editor's own text:\n{last}"
    );
    drop(supervisor);
}

/// The `thinkingLevelMap` (not the `reasoning` flag) is the capability signal:
/// `/effort` applies its declared level.
#[tokio::test]
async fn tui_effort_applies_on_a_map_addressable_model_without_the_reasoning_flag() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    std::fs::write(
        agent_dir.join("models.json"),
        serde_json::json!({
            "providers": {
                "test-provider": {
                    "api": "openai-completions",
                    "baseUrl": "http://127.0.0.1:9/v1",
                    "apiKey": "sk-test",
                    "models": [
                        { "id": "chat-plus", "name": "Chat Plus", "reasoning": false,
                          "thinkingLevelMap": { "off": null, "xhigh": "xhigh" },
                          "baseUrl": "http://127.0.0.1:9/v1", "contextWindow": 128_000,
                          "maxTokens": 4096 }
                    ]
                }
            }
        })
        .to_string(),
    )
    .expect("write models.json");
    let supervisor = spawn_supervisor(dir.path());
    let auth = pa_core::auth::AuthStorage::in_memory_without_env(
        &pa_core::auth::AuthStorageData::default(),
        std::sync::Arc::new(pa_core::auth::NoOAuth),
    );
    let mut registry = pa_core::models::ModelRegistry::create(auth, agent_dir.join("models.json"));
    registry.load_private_authorization_from_cache();
    let catalog: Vec<pa_types::ai::Model> = registry.get_available().into_iter().cloned().collect();
    assert_eq!(catalog.len(), 1, "the models.json model resolves available");
    assert_eq!(catalog[0].id, "chat-plus");

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: None,
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: catalog,
        model_configured_providers: ["test-provider".to_string()].into_iter().collect(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        client_settings: None,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            pa_tui::interactive::HeadlessStep::Submit("/model".to_string()),
            pa_tui::interactive::HeadlessStep::Type("chat".to_string()),
            pa_tui::interactive::HeadlessStep::Type("\n".to_string()),
            pa_tui::interactive::HeadlessStep::Submit("/effort xhigh".to_string()),
        ],
        width: 120,
        height: 36,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("Model: chat-plus"),
        "picking the model showed the TS confirm row:\n{rendered}"
    );
    assert!(
        rendered.contains("Thinking level: xhigh"),
        "the /effort command applied the map's addressable level:\n{rendered}"
    );
    assert!(
        !rendered.contains("Current model does not support thinking"),
        "a map-addressable model must not report the unsupported-model note:\n{rendered}"
    );

    let mut level_changes = 0;
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
            continue;
        }
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        level_changes += content
            .lines()
            .filter(|line| line.contains(r#""type":"thinking_level_change""#))
            .filter(|line| line.contains("xhigh"))
            .count();
    }
    assert!(
        level_changes >= 1,
        "the thinking_level_change row persisted at xhigh (saw {level_changes})"
    );
    drop(supervisor);
}

/// The skip (TS `CompactionSkippedError`) reaches the transcript through the
/// `compaction_end` event.
#[tokio::test]
async fn tui_compact_on_a_short_session_warns_nothing_to_compact() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    // The real agent engine over the faux provider: the skip path never reaches it.
    let script = serde_json::json!({ "engine": "faux", "responses": [] });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(dir.path().join("script.json")),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),

        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("/compact".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    if let Ok(dump) = std::env::var("PA_TUI_DUMP_FRAMES") {
        for (index, frame) in outcome.frames.iter().enumerate() {
            let _ = std::fs::write(
                std::path::Path::new(&dump).join(format!("skip-frame-{index:03}.txt")),
                frame,
            );
        }
    }
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("/compact"),
        "the session-command echo row rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Session is too short to compact"),
        "the skip warning rendered (TS compaction_end errorMessage):\n{rendered}"
    );
    let mut saw_compaction_entry = false;
    let mut saw_result_row = false;
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
            continue;
        }
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        saw_compaction_entry |= content.contains("\"type\":\"compaction\"");
        saw_result_row |= content.contains("\"session_slash_command_result\"");
    }
    assert!(
        !saw_compaction_entry,
        "a skipped compaction persisted no compaction entry"
    );
    assert!(
        !saw_result_row,
        "a skipped compaction persisted no result row"
    );
    drop(supervisor);
}

/// The loader row is a soft capture: a loaded box can batch the delayMs-paced
/// window past the paint loop.
#[tokio::test]
async fn tui_compact_shows_the_loader_then_the_summary_and_rebuilds() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    // The settings-pinned cut budget keeps the run small on the same keep-recent cut.
    std::fs::write(
        agent_dir.join("settings.json"),
        serde_json::json!({ "compaction": { "keepRecentTokens": 10 } }).to_string(),
    )
    .expect("write settings");
    let supervisor = spawn_supervisor(dir.path());

    // The session shape (TS-binary-verified): the second turn crosses the
    // budget at its user message, the third response is the summary, and its
    // 1.5s delay holds the loader window; a mid-turn cut would make TWO
    // summarizer calls, which this single-summary script does not serve.
    let filler = "history ".repeat(150);
    let script = serde_json::json!({
        "engine": "faux",
        "responses": [
            { "text": filler, "delayMs": 20 },
            { "text": "second turn done, kept intact" },
            { "text": "## Summary\nthe session story", "delayMs": 1500 },
        ],
    });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(dir.path().join("script.json")),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),

        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let ctrl_o = || {
        pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
            crossterm::event::KeyCode::Char('o'),
            crossterm::event::KeyModifiers::CONTROL,
        ))
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("first".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            pa_tui::interactive::HeadlessStep::Submit(
                "second, and please keep this second parity turn short and intact".to_string(),
            ),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            pa_tui::interactive::HeadlessStep::Submit("/compact focus on the goal".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            // Ctrl+O twice walks overview -> details -> all; the third press wraps to overview.
            ctrl_o(),
            ctrl_o(),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            ctrl_o(),
            pa_tui::interactive::HeadlessStep::SettleIdle,
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");

    if let Ok(dump) = std::env::var("PA_TUI_DUMP_FRAMES") {
        for (index, frame) in outcome.frames.iter().enumerate() {
            let _ = std::fs::write(
                std::path::Path::new(&dump).join(format!("compact-frame-{index:03}.txt")),
                frame,
            );
        }
    }
    let rendered = outcome.frames.join("\n");
    // The loader row is a soft, best-effort capture: a loaded box can batch the
    // delayMs-paced window past the paint loop, so no frame may show it; the
    // frame-level assertion belongs in a sandboxed run.
    let loader = "Compacting context (focus: focus on the goal)... (Ctrl+C to cancel)";
    let loader_frames = outcome
        .frames
        .iter()
        .filter(|frame| frame.contains(loader))
        .count();
    println!(
        "compaction loader evidence: {loader_frames} frames captured the loader row (soft check)"
    );
    assert!(
        rendered.contains("\u{25c6} Context compacted"),
        "the compaction summary header rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("the session story"),
        "the summary text rendered:\n{rendered}"
    );
    // The rebuilt transcript shows the retained second turn above the summary row.
    let last = outcome.frames.last().expect("the settled frame");
    assert!(
        last.contains("kept intact"),
        "the retained second turn heads the rebuilt transcript:\n{last}"
    );
    assert!(
        last.contains("\u{25c6} Context compacted"),
        "the summary row follows the retained tail:\n{last}"
    );
    assert!(
        !last.contains("first"),
        "the compacted-away first turn dropped from the rebuilt transcript:\n{last}"
    );

    // The expanded frames show the body and metadata; the wrap back to overview re-collapses.
    let expanded = outcome
        .frames
        .iter()
        .find(|frame| frame.contains("Compacted from"))
        .expect("some frame captured the expanded compaction block");
    assert!(
        expanded.contains("Compacted from") && expanded.contains("tokens"),
        "the expanded metadata row:\n{expanded}"
    );
    assert!(
        expanded.contains("\u{b7} focus: focus on the goal"),
        "the /compact focus rides the expanded metadata:\n{expanded}"
    );
    assert!(
        expanded.contains("Summary") && !expanded.contains("## Summary"),
        "the expanded body renders the summary markdown, not the EventSummary flatten:\n{expanded}"
    );
    assert!(
        !last.contains("Compacted from"),
        "the third Ctrl+O re-collapsed the block:\n{last}"
    );

    let mut saw_compaction_entry = false;
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
            continue;
        }
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        saw_compaction_entry |= content.contains("\"type\":\"compaction\"");
    }
    assert!(
        saw_compaction_entry,
        "the compaction entry persisted to the session file"
    );
    drop(supervisor);
}

#[tokio::test]
async fn tui_session_tree_navigates_forks_and_clones() {
    use crossterm::event::{KeyCode, KeyModifiers};

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({
        "engine": "faux",
        "responses": [
            { "text": "first answer", "delayMs": 10 },
            { "text": "second answer", "delayMs": 10 },
            { "text": "post-fork answer", "delayMs": 10 },
        ],
    });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(dir.path().join("script.json")),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        // The default tree filter keeps every message row visible, and the
        // branch-summary prompt is skipped (TS `branchSummary.skipPrompt`).
        tree_filter_mode: "default".to_string(),
        branch_summary_skip_prompt: true,
        show_images: true,
        fullscreen_mouse: true,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let key = |code: KeyCode| {
        pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
            code,
            KeyModifiers::NONE,
        ))
    };
    let enter = key(KeyCode::Enter);
    let up = key(KeyCode::Up);
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("first question".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            pa_tui::interactive::HeadlessStep::Submit("second question".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            // `/tree` opens the selector; Enter on the leaf is the TS no-op.
            pa_tui::interactive::HeadlessStep::Submit("/tree".to_string()),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            enter.clone(),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            // `/fork` opens the selector; Enter forks before the selected (latest) user message.
            pa_tui::interactive::HeadlessStep::Submit("/fork".to_string()),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            enter.clone(),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            // The fork re-entered the user message; submit runs it on the forked session.
            enter.clone(),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            // `/tree` again, then two rows up (the first answer) cuts the branch back.
            pa_tui::interactive::HeadlessStep::Submit("/tree".to_string()),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            up.clone(),
            up.clone(),
            enter.clone(),
            pa_tui::interactive::HeadlessStep::SettleIdle,
        ],
        width: 100,
        height: 34,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    if let Ok(dump) = std::env::var("PA_TUI_DUMP_FRAMES") {
        for (index, frame) in outcome.frames.iter().enumerate() {
            let _ = std::fs::write(
                std::path::Path::new(&dump).join(format!("tree-frame-{index:03}.txt")),
                frame,
            );
        }
    }
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("Session Tree"),
        "the tree pane rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Type to search:"),
        "the search line rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("user: first question") && rendered.contains("user: second question"),
        "the entry rows rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Already at this point"),
        "the leaf selection was a no-op:\n{rendered}"
    );
    assert!(
        rendered.contains("Fork from Message"),
        "the fork selector rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Forked to new session"),
        "the fork note rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("post-fork answer"),
        "the forked session ran a turn:\n{rendered}"
    );
    // The transcript rebuilt on the moved branch (the abandoned turn drops).
    assert!(
        rendered.contains("Navigated to selected point"),
        "the navigation note rendered:\n{rendered}"
    );
    let settled = outcome
        .frames
        .iter()
        .rev()
        .find(|frame| frame.contains("Navigated to selected point"))
        .expect("the navigation frame");
    assert!(
        !settled.contains("post-fork answer"),
        "the abandoned branch dropped from the rebuilt transcript:\n{settled}"
    );
    assert!(
        settled.contains("first answer"),
        "the moved branch kept the target path:\n{settled}"
    );
    let session_files: Vec<_> = std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
        .filter(|entry| entry.path().extension().and_then(|e| e.to_str()) == Some("jsonl"))
        .collect();
    assert!(
        session_files.len() >= 2,
        "the fork wrote a new session file: {} files",
        session_files.len()
    );
    drop(supervisor);
}

/// Two ~12k-token unpaced turns must render at the producer's rate, not a frame-rate ceiling.
#[tokio::test]
async fn tui_big_streamed_turns_render_at_the_producer_rate() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    // Two ~12k-token fillers; each segment carries a MARK-nn marker for the mid-turn progression.
    let mut filler = String::new();
    for segment in 0..24 {
        let _ = write!(filler, "MARK-{segment:02} ");
        filler.push_str(&"history ".repeat(250));
    }
    // Paced at 3000 tokens/second so the turn streams for ~4s: the mid-turn
    // progression assertion needs several wire updates inside the turn (an
    // unpaced faux finishes in ~0.3s). The 45s bound clears a starved render.
    let script = serde_json::json!({
        "engine": "faux",
        "tokensPerSecond": 3_000,
        "responses": [
            { "text": filler.clone() },
            { "text": format!("{filler}second big turn done, tail marker intact") },
        ],
    });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(dir.path().join("script.json")),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),

        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("first".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 45_000 },
            pa_tui::interactive::HeadlessStep::Submit("second".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 45_000 },
        ],
        width: 100,
        height: 30,
    };
    let started = Instant::now();
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let wall = started.elapsed();

    if let Ok(dump) = std::env::var("PA_TUI_DUMP_FRAMES") {
        for (index, frame) in outcome.frames.iter().enumerate() {
            let _ = std::fs::write(
                std::path::Path::new(&dump).join(format!("stream-frame-{index:03}.txt")),
                frame,
            );
        }
    }
    let rendered = outcome.frames.join("\n");
    assert!(
        !rendered.contains("timed out waiting for the turn to finish"),
        "a WaitIdle barrier expired; the render starved behind the stream:\n{rendered}"
    );
    // Both turns settled full (the window follows the tail: the last marker is the proof).
    assert!(
        rendered.contains("tail marker intact"),
        "the second big turn fully rendered:\n{rendered}"
    );
    assert_eq!(
        outcome.last_assistant_text.as_deref(),
        Some(format!("{filler}second big turn done, tail marker intact").as_str()),
        "the final assistant text is the full second turn"
    );
    // The applied content progressed mid-turn (a starved pipeline shows it once at its end).
    let marks: std::collections::BTreeSet<String> = outcome
        .frames
        .iter()
        .flat_map(|frame| frame.lines())
        .flat_map(|line| line.split_whitespace())
        .filter(|word| word.starts_with("MARK-"))
        .map(std::string::ToString::to_string)
        .collect();
    assert!(
        marks.len() >= 5,
        "only {len} segment markers ever rendered mid-turn (needs >= 5); the applied stream starved",
        len = marks.len()
    );
    assert!(
        wall < Duration::from_secs(100),
        "the whole run took {wall:?}; the turn render must keep up with the producer"
    );
    drop(supervisor);
}

/// A settings fixture rebinding `app.tools.expand` to `ctrl+alt+x` drives the surface.
#[tokio::test]
async fn tui_renders_and_fires_user_keybindings_from_settings() {
    use crossterm::event::{KeyCode, KeyModifiers};

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    // One binding overridden exactly like a user's `~/.prime/agent/keybindings.json` would.
    std::fs::write(
        agent_dir.join("keybindings.json"),
        r#"{ "app.tools.expand": "ctrl+alt+x" }"#,
    )
    .expect("write keybindings.json");
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({
        "engine": "faux",
        "responses": [{ "text": "scripted reply", "delayMs": 10 }],
    });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(dir.path().join("script.json")),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::new(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        // The exact load path the CLI uses: the fixture overrides the default set.
        keybindings: pa_tui::keybindings::KeybindingsManager::create(&agent_dir),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let key = |code: KeyCode, modifiers: KeyModifiers| {
        pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(code, modifiers))
    };
    let ctrl_alt_x = key(
        KeyCode::Char('x'),
        KeyModifiers::CONTROL | KeyModifiers::ALT,
    );
    let ctrl_o = key(KeyCode::Char('o'), KeyModifiers::CONTROL);
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("hello".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            ctrl_alt_x,
            // The default key must no longer fire it (a second cycle reaches "all").
            ctrl_o,
            // The info panel mounts over the dock (operator directive 2026-09-26);
            // End jumps to the document's bottom, Esc closes it.
            pa_tui::interactive::HeadlessStep::Submit("/hotkeys".to_string()),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Move cursor / browse history".to_string(),
                timeout_ms: 30_000,
            },
            pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
                crossterm::event::KeyCode::End,
                crossterm::event::KeyModifiers::NONE,
            )),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "mouse click on link".to_string(),
                timeout_ms: 30_000,
            },
            pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
                crossterm::event::KeyCode::Esc,
                crossterm::event::KeyModifiers::NONE,
            )),
            pa_tui::interactive::HeadlessStep::WaitGone {
                needle: "scroll \u{b7} Esc close".to_string(),
                timeout_ms: 30_000,
            },
        ],
        width: 120,
        // Tall enough that the `/hotkeys` panel holds a real window of the guide.
        height: 60,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");

    // The hint renders the user's binding, not the default (operator directive 2026-09-28).
    assert!(
        rendered.contains("Collapsed mode (Ctrl+Alt+X to expand)"),
        "the prompt-context hint renders the override:\n{rendered}"
    );
    assert!(
        rendered.contains("Details mode (Ctrl+Alt+X to expand)"),
        "the override key cycled conversation detail:\n{rendered}"
    );
    assert!(
        !rendered.contains("Expanded mode (Ctrl+Alt+X to collapse)"),
        "the default ctrl+o must not cycle after the override:\n{rendered}"
    );
    assert!(
        rendered.contains("scripted reply"),
        "the scripted turn rendered:\n{rendered}"
    );
    // End jumped the window to the document's bottom (the override row has unit tests).
    assert!(
        rendered.contains("Move cursor / browse history"),
        "the hotkeys panel rendered the guide:\n{rendered}"
    );
    assert!(
        rendered.contains("mouse click on link"),
        "the End key jumped the panel to the guide's bottom:\n{rendered}"
    );
    // The removed default key is gone (no other default binding uses ctrl+o).
    assert!(
        !rendered.contains("Ctrl+O"),
        "the hotkeys guide must not show the removed default:\n{rendered}"
    );
    // The guide stayed out of the transcript (the operator's no-flooding
    // directive): the last frame holds the reply and the dock only.
    let last = outcome.frames.last().expect("frames");
    assert!(
        !last.contains("Move cursor / browse history"),
        "the hotkeys guide never lands in the transcript:\n{last}"
    );
    assert!(
        last.contains("Details mode (Ctrl+Alt+X to expand)"),
        "the dock returned after the panel closed:\n{last}"
    );
    drop(supervisor);
}

/// The visible follow-up queue (TS `queuedMessagesContainer`): prompts
/// submitted while a turn runs park as dim preview rows with the browse hint.
#[tokio::test]
async fn tui_prompts_queued_behind_a_turn_render_the_queue_strip() {
    use crossterm::event::{KeyCode, KeyModifiers};
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    // The first turn holds for 1.5s (`delayMs`): the parked submissions land
    // inside that window (the runner flips `busy` long before the 750ms barrier).
    let script_path = dir.path().join("script.json");
    let script = serde_json::json!({ "responses": [
        { "text": "first turn", "delayMs": 1500 },
        { "text": "steered delivery" },
        { "text": "followed up delivery" },
    ]});
    std::fs::write(&script_path, script.to_string()).expect("write script");

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path.clone()),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("start the slow turn".to_string()),
            // Half-way through the hold the turn is running: the parked prompts queue behind it.
            pa_tui::interactive::HeadlessStep::WaitMs(750),
            pa_tui::interactive::HeadlessStep::Submit("steering prompt".to_string()),
            pa_tui::interactive::HeadlessStep::Type("follow-up prompt".to_string()),
            pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
                KeyCode::Enter,
                KeyModifiers::ALT,
            )),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 60_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");

    assert!(!outcome.frames.is_empty(), "frames were captured");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("Steering: steering prompt"),
        "the steering preview rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Follow-up: follow-up prompt"),
        "the follow-up preview rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("to browse and edit queued messages"),
        "the browse hint rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("steered delivery"),
        "the steering prompt delivered:\n{rendered}"
    );
    assert!(
        rendered.contains("followed up delivery"),
        "the follow-up prompt delivered:\n{rendered}"
    );
    let last = outcome.frames.last().expect("a final frame");
    assert!(
        !last.contains("to browse and edit queued messages")
            && !last.contains("Steering: ")
            && !last.contains("Follow-up: "),
        "the queue strip cleared after delivery:\n{last}"
    );
    drop(supervisor);
}

/// The live-dogfood failure pair (Kevin's repro, 2026-09-21): the flagged
/// model resolves from the full catalog and fails at run-start auth
/// validation with the TS login-guidance message — never "Model not found".
#[tokio::test]
async fn tui_flagged_model_turn_reports_the_ts_preflight_error_without_credentials() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());
    // No models.json and no credentials (ambient keys stripped), so the
    // auth-scoped catalog is empty but the bundled catalog carries the model.
    let glm: pa_types::ai::Model = serde_json::from_value(serde_json::json!({
        "id": "z-ai/glm-5.3", "name": "GLM 5.3", "api": "openai-completions",
        "provider": "prime-inference", "baseUrl": "https://inference.example/v1",
        "reasoning": true, "input": ["text"],
        "cost": { "input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0 },
        "contextWindow": 128_000, "maxTokens": 8192
    }))
    .expect("catalog entry");
    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: None,
        model_selection: pa_tui::interactive::ModelSelection {
            provider: Some("prime-inference".to_string()),
            model: Some("z-ai/glm-5.3".to_string()),
            ..Default::default()
        },
        model_catalog: vec![glm],
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        client_settings: None,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            pa_tui::interactive::HeadlessStep::Submit("/model".to_string()),
            pa_tui::interactive::HeadlessStep::Type("glm".to_string()),
            pa_tui::interactive::HeadlessStep::Type("\n".to_string()),
            pa_tui::interactive::HeadlessStep::Submit("hello".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 60_000 },
        ],
        width: 120,
        height: 36,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("must be configured externally") && rendered.contains("prime-inference"),
        "the pick against the empty auth-scoped catalog routes the sign-in flow (no provider-auth hook in this composition, so the TS external-config error):\n{rendered}"
    );
    assert!(
        !rendered.contains("Model not found: "),
        "the not-signed-in pick never surfaces the dead-end refusal (the typed sign-in class):\n{rendered}"
    );
    assert!(
        rendered.contains("No API key found for prime-inference"),
        "the turn resolves the flagged model from the full catalog and fails at the run-start auth validation with the TS message:\n{rendered}"
    );
    assert!(
        !rendered.contains("No models available"),
        "the auth-blind turn resolution must not report the resolver's empty-catalog error:\n{rendered}"
    );
    let last = outcome.frames.last().expect("a final frame");
    // The failed pick must hold the `model:effort` label (TS
    // `getModelContextLabel`) with glm-5.3's effective level.
    assert!(
        last.contains("z-ai/glm-5.3:high ·"),
        "the footer label holds the resolved flagged model (the failed pick switched nothing):\n{last}"
    );
    drop(supervisor);
}

/// The dogfood acceptance: a `/model` pick must move the footer label immediately
/// and leave the next turn on the switched model.
#[tokio::test]
async fn tui_model_pick_refreshes_the_label_and_the_next_turn_resolves() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    std::fs::write(
        agent_dir.join("models.json"),
        serde_json::json!({
            "providers": {
                "test-provider": {
                    "api": "openai-completions",
                    "baseUrl": "http://127.0.0.1:9/v1",
                    "apiKey": "sk-test",
                    "models": [
                        { "id": "mock-1", "name": "Mock 1", "api": "openai-completions",
                          "baseUrl": "http://127.0.0.1:9/v1", "contextWindow": 128_000,
                          "maxTokens": 4096 },
                        { "id": "mock-2", "name": "Mock 2", "api": "openai-completions",
                          "baseUrl": "http://127.0.0.1:9/v1", "contextWindow": 128_000,
                          "maxTokens": 4096 }
                    ]
                }
            }
        })
        .to_string(),
    )
    .expect("write models.json");
    // Retries off (the default 3-attempt retry chain would hold the turn busy
    // past the idle window): the post-switch turn fails once and settles.
    std::fs::write(
        agent_dir.join("settings.json"),
        serde_json::json!({ "retry": { "enabled": false } }).to_string(),
    )
    .expect("write settings.json");
    let supervisor = spawn_supervisor(dir.path());
    // The client-side snapshot over the same registry scope as the daemon's (hermetic auth).
    let auth = pa_core::auth::AuthStorage::in_memory_without_env(
        &pa_core::auth::AuthStorageData::default(),
        std::sync::Arc::new(pa_core::auth::NoOAuth),
    );
    let mut registry = pa_core::models::ModelRegistry::create(auth, agent_dir.join("models.json"));
    registry.load_private_authorization_from_cache();
    let catalog: Vec<pa_types::ai::Model> = registry.get_available().into_iter().cloned().collect();
    assert_eq!(
        catalog.len(),
        2,
        "both models.json models resolve available"
    );
    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: None,
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: catalog,
        model_configured_providers: ["test-provider".to_string()].into_iter().collect(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        client_settings: None,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            pa_tui::interactive::HeadlessStep::Submit("/model".to_string()),
            pa_tui::interactive::HeadlessStep::Type("mock-2".to_string()),
            pa_tui::interactive::HeadlessStep::Type("\n".to_string()),
            pa_tui::interactive::HeadlessStep::Submit("turn after the switch".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 90_000 },
        ],
        width: 120,
        height: 36,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("Model: mock-2"),
        "the pick applied through the daemon set_model switch:\n{rendered}"
    );
    assert!(
        !rendered.contains("No models available"),
        "the switched model must resolve for the next turn:\n{rendered}"
    );
    assert!(
        rendered.contains("Error: Connection error."),
        "the post-switch turn reached the dead provider (not a resolution failure):\n{rendered}"
    );
    let last = outcome.frames.last().expect("a final frame");
    assert!(
        last.contains("mock-2 ·"),
        "the footer label refreshed to the picked model:\n{last}"
    );
    drop(supervisor);
}

/// `--models` scope end to end: the cycle keys walk the session's scope
/// order, not the catalog's (mock-2 must never appear).
#[tokio::test]
async fn tui_scoped_models_cycle_through_the_session_scope() {
    use crossterm::event::{KeyCode, KeyModifiers};

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    std::fs::write(
        agent_dir.join("models.json"),
        serde_json::json!({
            "providers": {
                "test-provider": {
                    "api": "openai-completions",
                    "baseUrl": "http://127.0.0.1:9/v1",
                    "apiKey": "sk-test",
                    "models": [
                        { "id": "mock-1", "name": "Mock 1", "api": "openai-completions",
                          "baseUrl": "http://127.0.0.1:9/v1", "contextWindow": 128_000,
                          "maxTokens": 4096 },
                        { "id": "mock-2", "name": "Mock 2", "api": "openai-completions",
                          "baseUrl": "http://127.0.0.1:9/v1", "contextWindow": 128_000,
                          "maxTokens": 4096 },
                        { "id": "mock-3", "name": "Mock 3", "api": "openai-completions",
                          "baseUrl": "http://127.0.0.1:9/v1", "contextWindow": 128_000,
                          "maxTokens": 4096 }
                    ]
                }
            }
        })
        .to_string(),
    )
    .expect("write models.json");
    let supervisor = spawn_supervisor(dir.path());
    let mut options = base_options(&supervisor, dir.path(), &session_dir);
    // No scripted engine: the real startup chain resolves the session's model
    // (the scripted engine answers none and the cycle would refuse to switch).
    options.script_path = None;
    options.models = Some(vec![
        "test-provider/mock-1".to_string(),
        "test-provider/mock-3".to_string(),
    ]);
    let key = |code: KeyCode, modifiers: KeyModifiers| {
        pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(code, modifiers))
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            // alt+m cycles forward: mock-1 -> mock-3 (unscoped would show mock-2).
            key(KeyCode::Char('m'), KeyModifiers::ALT),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Model: test-provider/mock-3".to_string(),
                timeout_ms: 30_000,
            },
            key(KeyCode::Char('m'), KeyModifiers::SHIFT | KeyModifiers::ALT),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Model: test-provider/mock-1".to_string(),
                timeout_ms: 30_000,
            },
        ],
        width: 120,
        height: 36,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("Model: test-provider/mock-3"),
        "alt+m cycled forward through the scope:\n{rendered}"
    );
    assert!(
        !rendered.contains("Model: test-provider/mock-2"),
        "the unscoped catalog order never surfaced:\n{rendered}"
    );
    assert!(
        rendered.contains("Model: test-provider/mock-1"),
        "shift+alt+m cycled backward through the scope:\n{rendered}"
    );
    drop(supervisor);
}

/// The base options every utility-command verifier shares.
fn base_options(
    supervisor: &Supervisor,
    dir: &Path,
    session_dir: &Path,
) -> pa_tui::interactive::InteractiveOptions {
    pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.to_path_buf(),
        session_dir: Some(session_dir.to_path_buf()),
        script_path: Some(dir.join("script.json")),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
        provider_auth: None,
        traces: None,
        update_commands: None,
    }
}

#[tokio::test]
async fn tui_renames_session_through_slash_command() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({ "engine": "faux", "responses": [
        { "text": "scripted reply" },
    ] });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let options = base_options(&supervisor, dir.path(), &session_dir);
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            // Set through the alias: /rename resolves to /name.
            pa_tui::interactive::HeadlessStep::Submit("/rename my session".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            pa_tui::interactive::HeadlessStep::Submit("/name".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 120,
        height: 36,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    if let Ok(dir) = std::env::var("PA_TUI_DUMP_FRAMES") {
        for (index, frame) in outcome.frames.iter().enumerate() {
            let _ = std::fs::write(
                std::path::Path::new(&dir).join(format!("frame-{index:03}.txt")),
                frame,
            );
        }
    }
    assert!(
        rendered.contains("Session name set: my session"),
        "the /rename status row rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Session name: my session"),
        "the /name report row rendered:\n{rendered}"
    );
    // The session file carries the `session_info` entry with the name.
    let mut saw_name = false;
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
            continue;
        }
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        for line in content.lines() {
            if let Ok(value) = serde_json::from_str::<serde_json::Value>(line) {
                if value["type"] == "session_info" && value["name"] == "my session" {
                    saw_name = true;
                }
            }
        }
    }
    assert!(
        saw_name,
        "the session_info entry persisted (the /name arm reaches the rename machinery)"
    );
    // The daemon state reports the name (the summary the roster and agents view read).
    let (client, _client_events) = pa_tui::daemon_client::DaemonClient::connect(&supervisor.socket)
        .await
        .expect("connect");
    let state = client
        .request_ok(DaemonCommand::GetState {
            id: None,
            active_session_id: outcome.active_session_id.clone(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_state");
    assert_eq!(state["sessionName"], "my session");
}

// `/new` with arguments (TS `parseNewSessionCommand`): the `--name` value renames
// the new session through the `/name` path, the verbatim suffix submits as the
// initial prompt, and a parse error keeps the draft without creating a session.
#[tokio::test]
async fn tui_new_command_honors_name_and_prompt() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());
    // Each created session replays the script from its first response, so one
    // shared reply serves both prompt-bearing runs.
    let script = serde_json::json!({ "engine": "faux", "responses": [
        { "text": "scripted reply" },
    ] });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let options = base_options(&supervisor, dir.path(), &session_dir);
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            // A parse error must not create a session.
            pa_tui::interactive::HeadlessStep::Submit(
                r#"/new --name "one" --name "two""#.to_string(),
            ),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            // `--name` plus the `--`-separated initial prompt.
            pa_tui::interactive::HeadlessStep::Submit(
                r#"/new --name "my session" -- do something"#.to_string(),
            ),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            // A bare suffix is the initial prompt; the session stays unnamed.
            pa_tui::interactive::HeadlessStep::Submit("/new plain prompt".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            // A name without a prompt names the new session and submits nothing.
            pa_tui::interactive::HeadlessStep::Submit("/new --name solo".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 120,
        height: 36,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains(r#"Duplicate /new option: "--name""#),
        "the duplicate --name error row rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("do something"),
        "the initial prompt's user message rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("plain prompt"),
        "the bare-suffix prompt's user message rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("scripted reply"),
        "the scripted replies rendered:\n{rendered}"
    );
    // The create arm persists each session's file eagerly, so one file per
    // created session: the startup session plus the three /new runs; the
    // parse error added none.
    let mut files: Vec<(String, String)> = Vec::new();
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
            continue;
        }
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        let id = content
            .lines()
            .next()
            .and_then(|header| serde_json::from_str::<serde_json::Value>(header).ok())
            .and_then(|header| header["id"].as_str().map(str::to_string))
            .unwrap_or_default();
        files.push((id, content));
    }
    assert_eq!(files.len(), 4, "the parse error created no session");
    // The name-and-prompt run: its file carries the initial prompt, the
    // scripted reply, and the session_info rename.
    let (named_id, named) = files
        .iter()
        .find(|(_, content)| content.contains("do something"))
        .map(|(id, content)| (id.clone(), content.as_str()))
        .expect("the named run persisted the initial prompt");
    assert!(
        named.contains("scripted reply"),
        "the scripted reply persisted alongside the prompt"
    );
    assert!(
        named.lines().any(|line| {
            serde_json::from_str::<serde_json::Value>(line)
                .is_ok_and(|value| value["type"] == "session_info" && value["name"] == "my session")
        }),
        "the session_info row named the new session"
    );
    // The bare-suffix run: the prompt submitted, the session left unnamed.
    let (_, bare) = files
        .iter()
        .find(|(_, content)| content.contains("plain prompt"))
        .map(|(id, content)| (id.clone(), content.as_str()))
        .expect("the bare-suffix run persisted the initial prompt");
    assert!(
        bare.contains("scripted reply"),
        "the bare-suffix scripted reply persisted"
    );
    assert!(
        bare.lines().all(|line| {
            serde_json::from_str::<serde_json::Value>(line)
                .map_or(true, |value| value["type"] != "session_info")
        }),
        "the bare-suffix session stayed unnamed"
    );
    // The name-only run: named, and no prompt ever submitted.
    let (_, solo) = files
        .iter()
        .find(|(_, content)| {
            content.lines().any(|line| {
                serde_json::from_str::<serde_json::Value>(line)
                    .is_ok_and(|value| value["type"] == "session_info" && value["name"] == "solo")
            })
        })
        .map(|(id, content)| (id.clone(), content.as_str()))
        .expect("the name-only run persisted the rename");
    assert!(
        solo.lines().all(|line| {
            serde_json::from_str::<serde_json::Value>(line)
                .map_or(true, |value| value["type"] != "message")
        }),
        "the name-only run submitted no prompt"
    );
    // The daemon state reports the names (the roster summary's field).
    let (client, _client_events) = pa_tui::daemon_client::DaemonClient::connect(&supervisor.socket)
        .await
        .expect("connect");
    let state = client
        .request_ok(DaemonCommand::GetState {
            id: None,
            active_session_id: named_id,
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_state");
    assert_eq!(state["sessionName"], "my session");
    let state = client
        .request_ok(DaemonCommand::GetState {
            id: None,
            active_session_id: outcome.active_session_id.clone(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_state");
    assert_eq!(state["sessionName"], "solo");
}

#[tokio::test]
async fn tui_side_question_pane_flow() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({ "engine": "faux", "responses": [
        { "text": "Paris, obviously" },
        { "text": "Second answer" },
    ] });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let options = base_options(&supervisor, dir.path(), &session_dir);
    let escape = pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
        crossterm::event::KeyCode::Esc,
        crossterm::event::KeyModifiers::NONE,
    ));
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit(
                "/btw what is the capital of France".to_string(),
            ),
            // The side question runs outside the turn state (WaitIdle cannot
            // see it): wait for the rendered condition, not a fixed window.
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Paris, obviously".to_string(),
                timeout_ms: 30_000,
            },
            // The pane must SETTLE first: TS's active-run guard drops a
            // follow-up submitted mid-stream; the settled hint row is the marker.
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "reply to follow up".to_string(),
                timeout_ms: 30_000,
            },
            pa_tui::interactive::HeadlessStep::SettleIdle,
            // The open pane captures a plain reply as a follow-up side question.
            pa_tui::interactive::HeadlessStep::Submit("and its largest city".to_string()),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Second answer".to_string(),
                timeout_ms: 30_000,
            },
            // Settle again: an Esc mid-run would CANCEL the run instead of
            // closing the pane (TS's two-stage escape).
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "reply to follow up".to_string(),
                timeout_ms: 30_000,
            },
            pa_tui::interactive::HeadlessStep::SettleIdle,
            pa_tui::interactive::HeadlessStep::Submit("/model".to_string()),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Slash commands are not available in side conversations.".to_string(),
                timeout_ms: 30_000,
            },
            pa_tui::interactive::HeadlessStep::SettleIdle,
            // Esc returns to the main thread: wait for the pane's hint row to leave the frame.
            escape,
            pa_tui::interactive::HeadlessStep::WaitGone {
                needle: "esc to return to session".to_string(),
                timeout_ms: 10_000,
            },
            pa_tui::interactive::HeadlessStep::SettleIdle,
        ],
        width: 120,
        height: 36,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    if let Ok(dir) = std::env::var("PA_TUI_DUMP_FRAMES") {
        for (index, frame) in outcome.frames.iter().enumerate() {
            let _ = std::fs::write(
                std::path::Path::new(&dir).join(format!("frame-{index:03}.txt")),
                frame,
            );
        }
    }
    assert!(
        rendered.contains("/btw  what is the capital of France"),
        "the pane rendered the /btw header:\n{rendered}"
    );
    assert!(
        rendered.contains("Paris, obviously"),
        "the streamed answer rendered in the pane:\n{rendered}"
    );
    assert!(
        rendered.contains("and its largest city"),
        "the follow-up question rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("Second answer"),
        "the follow-up answer rendered:\n{rendered}"
    );
    assert!(
        rendered.contains(
            "Slash commands are not available in side conversations. Press esc to return to the main thread."
        ),
        "the in-pane slash notice rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("reply to follow up · esc to return to session"),
        "the pane hint rendered:\n{rendered}"
    );
    let last = outcome.frames.last().expect("a final frame");
    assert!(
        !last.contains("esc to return to session"),
        "esc closed the pane:\n{last}"
    );
    // Side questions are not durable: the session file has no side-question user rows.
    let mut leaked = false;
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
            continue;
        }
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        leaked |= content.contains("what is the capital of France");
    }
    assert!(!leaked, "the side question stayed out of the session file");
}

#[tokio::test]
async fn tui_settings_menu_cycles_rows() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({ "engine": "faux", "responses": [] });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let options = base_options(&supervisor, dir.path(), &session_dir);
    let enter = || {
        pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
            crossterm::event::KeyCode::Enter,
            crossterm::event::KeyModifiers::NONE,
        ))
    };
    let escape = || {
        pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
            crossterm::event::KeyCode::Esc,
            crossterm::event::KeyModifiers::NONE,
        ))
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("/settings".to_string()),
            pa_tui::interactive::HeadlessStep::WaitMs(500),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            // Enter on the first row (Auto-compact) cycles it to false (`set_auto_compaction`).
            enter(),
            pa_tui::interactive::HeadlessStep::WaitMs(500),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            escape(),
            pa_tui::interactive::HeadlessStep::WaitMs(300),
        ],
        width: 120,
        height: 36,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    if let Ok(dir) = std::env::var("PA_TUI_DUMP_FRAMES") {
        for (index, frame) in outcome.frames.iter().enumerate() {
            let _ = std::fs::write(
                std::path::Path::new(&dir).join(format!("frame-{index:03}.txt")),
                frame,
            );
        }
    }
    assert!(
        rendered.contains("Auto-compact"),
        "the settings menu rendered its first row:\n{rendered}"
    );
    assert!(
        rendered.contains("1 General    2 Models    3 Display    4 Editor    5 Agents"),
        "the settings menu rendered its tab strip:\n{rendered}"
    );
    assert!(
        rendered.contains("Type to search · Tab/1-5 tabs · ←/→/Enter/Space change · Esc close"),
        "the settings hint rendered:\n{rendered}"
    );
}

/// The operator's Esc-ordering pin (2026-09-25): Esc closes the cwd
/// completion menu without interrupting the streaming turn.
#[tokio::test]
async fn tui_esc_closes_the_completion_menu_without_interrupting_the_turn() {
    use crossterm::event::KeyCode;

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    std::fs::create_dir_all(dir.path().join(".claude")).expect("dot dir");
    std::fs::write(dir.path().join("main.rs"), "fn main() {}").expect("write");
    std::fs::write(dir.path().join("notes.md"), "notes").expect("write");
    // A fast text block marks it streaming; the slow thinking block holds the menu window.
    let script = serde_json::json!({
        "engine": "faux",
        "tokensPerSecond": 4,
        "responses": [
            { "content": [
                { "type": "text", "text": "the turn is streaming" },
                { "type": "thinking",
                  "thinking": "a long slow thinking pass keeps the turn streaming while the completion menu opens and escape closes it" },
                { "type": "text", "text": "the final answer streams after the menu check" },
            ] },
        ],
    });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let supervisor = spawn_supervisor(dir.path());
    let options = base_options(&supervisor, dir.path(), &session_dir);
    let key = |code: KeyCode| {
        pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(
            code,
            crossterm::event::KeyModifiers::NONE,
        ))
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("start the long turn".to_string()),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "the turn is streaming".to_string(),
                timeout_ms: 30_000,
            },
            // The editor stays live during the turn: `./` + Tab opens the menu over it.
            pa_tui::interactive::HeadlessStep::Type("./".to_string()),
            key(KeyCode::Tab),
            pa_tui::interactive::HeadlessStep::SettleIdle,
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "notes.md".to_string(),
                timeout_ms: 30_000,
            },
            // Esc closes the menu; the turn keeps running (a leaked abort would kill it).
            key(KeyCode::Esc),
            pa_tui::interactive::HeadlessStep::WaitGone {
                needle: "notes.md".to_string(),
                timeout_ms: 10_000,
            },
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 60_000 },
        ],
        width: 100,
        height: 34,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("main.rs") && rendered.contains("notes.md"),
        "the cwd completion menu listed the non-hidden entries:\n{rendered}"
    );
    assert!(
        !rendered.contains(".claude"),
        "the dot dir never lists in the cwd browse:\n{rendered}"
    );
    assert!(
        rendered.contains("the final answer streams after the menu check"),
        "Esc closed the menu and the turn ran to its final answer (no leaked abort):\n{rendered}"
    );
    drop(supervisor);
}

/// Prompt-stash verifier: a draft in the editor belongs to the session it was typed in.
#[tokio::test]
async fn tui_prompt_stash_round_trips_across_in_place_switch() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({
        "engine": "faux",
        "responses": [{ "text": "stash switch reply", "delayMs": 10 }],
    });
    let script_path = dir.path().join("script.json");
    let first = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;
    let second = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        provider_auth: None,
        traces: None,
        update_commands: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path.clone()),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::Attach(first.clone()),
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        client_settings: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        prompt_stash: std::sync::Arc::default(),
        session_rlm_depth: None,
        session_has_children: false,
        restore_dock_focus: false,
    };
    let enter = || {
        pa_tui::interactive::HeadlessStep::Key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE))
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Type("f24 stash draft hello".to_string()),
            pa_tui::interactive::HeadlessStep::Submit(format!("/switch {second}")),
            // Enter submits nothing; the draft never bleeds into session B.
            enter(),
            pa_tui::interactive::HeadlessStep::WaitMs(500),
            pa_tui::interactive::HeadlessStep::Submit(format!("/switch {first}")),
            enter(),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("Restored stashed prompt"),
        "the switch-back restored the stashed draft:\n{rendered}"
    );

    // Daemon-side: the restored draft ran on session A; B never received it.
    let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(&supervisor.socket)
        .await
        .expect("connect supervisor");
    let last_first = client
        .request_ok(DaemonCommand::GetLastAssistantText {
            id: None,
            active_session_id: first.clone(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_last_assistant_text on the first session");
    assert_eq!(
        last_first["text"], "stash switch reply",
        "the restored draft submitted to the session it belongs to"
    );
    let last_second = client
        .request_ok(DaemonCommand::GetLastAssistantText {
            id: None,
            active_session_id: second.clone(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_last_assistant_text on the second session");
    assert_eq!(
        last_second["text"],
        serde_json::Value::Null,
        "the stashed draft never leaked into the switched-to session"
    );
    client.close();
    drop(supervisor);
}

/// Prompt-stash verifier, the agents-view handoff arm: the (user-rebound)
/// `app.session.resume` key stashes the draft; reopening the chat restores it.
#[tokio::test]
async fn tui_prompt_stash_survives_the_agents_view_handoff() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    // `app.session.resume` has no default key, so the fixture binds it to a
    // plain key (both products fire the action with text in the editor).
    std::fs::write(
        agent_dir.join("keybindings.json"),
        r#"{ "app.session.resume": "f2" }"#,
    )
    .expect("write keybindings.json");
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({
        "engine": "faux",
        "responses": [{ "text": "handoff restore reply", "delayMs": 10 }],
    });
    let script_path = dir.path().join("script.json");
    let first = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;

    // The draft carries a pasted image, so the handoff must round-trip the
    // bytes (the reopened chat's paste registry is empty — the stash hydrates it).
    let png_path = dir.path().join("fixture.png");
    std::fs::write(&png_path, MINIMAL_PNG).expect("write fixture image");
    std::env::set_var("PRIME_AGENT_TEST_CLIPBOARD_IMAGE", &png_path);
    let prompt_stash: std::sync::Arc<std::sync::Mutex<pa_tui::prompt_stash::PromptStashStore>> =
        std::sync::Arc::default();
    let make_options = || pa_tui::interactive::InteractiveOptions {
        models: None,
        provider_auth: None,
        traces: None,
        update_commands: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path.clone()),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::Attach(first.clone()),
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        client_settings: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::create(&agent_dir),
        prompt_stash: prompt_stash.clone(),
        session_rlm_depth: None,
        session_has_children: false,
        restore_dock_focus: false,
    };

    // Run one: the draft is typed, then the resume key hands the pane to the agents view.
    let plan_one = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Key(KeyEvent::new(
                KeyCode::Char('v'),
                KeyModifiers::CONTROL,
            )),
            pa_tui::interactive::HeadlessStep::WaitMs(300),
            pa_tui::interactive::HeadlessStep::Type(" f24 handoff draft".to_string()),
            pa_tui::interactive::HeadlessStep::Key(KeyEvent::new(
                KeyCode::F(2),
                KeyModifiers::NONE,
            )),
        ],
        width: 100,
        height: 30,
    };
    let outcome_one = run_headless_bounded(make_options(), plan_one)
        .await
        .expect("interactive run one");
    assert!(
        outcome_one.return_to_agents_view,
        "the resume key hands the pane to the agents view"
    );

    let plan_two = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::WaitMs(300),
            pa_tui::interactive::HeadlessStep::Key(KeyEvent::new(
                KeyCode::Enter,
                KeyModifiers::NONE,
            )),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome_two = run_headless_bounded(make_options(), plan_two)
        .await
        .expect("interactive run two");
    let rendered = outcome_two.frames.join("\n");
    assert!(
        rendered.contains("Restored stashed prompt"),
        "the reopened chat restored the stashed draft:\n{rendered}"
    );
    assert!(
        rendered.contains("[image #1]"),
        "the restored draft carries its image marker:\n{rendered}"
    );

    // The persisted message carries the image: the registry held it only through the hydrate.
    let mut persisted_with_image = false;
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) == Some("jsonl") {
            if let Ok(text) = std::fs::read_to_string(&path) {
                persisted_with_image |= text.contains("image/png");
            }
        }
    }
    assert!(
        persisted_with_image,
        "the restored draft attached the stashed image bytes on submit"
    );

    let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(&supervisor.socket)
        .await
        .expect("connect supervisor");
    let last = client
        .request_ok(DaemonCommand::GetLastAssistantText {
            id: None,
            active_session_id: first.clone(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_last_assistant_text");
    assert_eq!(
        last["text"], "handoff restore reply",
        "the restored draft submitted after the handoff"
    );
    client.close();
    drop(supervisor);
}

/// Prompt-stash verifier, the pasted-image arm: the stashed draft round-trips its image bytes.
#[tokio::test]
async fn tui_prompt_stash_restores_a_pasted_image_with_the_draft() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    // A one-pixel PNG: the fixture stands in for the system clipboard (no display server).
    let png_path = dir.path().join("fixture.png");
    std::fs::write(&png_path, MINIMAL_PNG).expect("write fixture image");
    // The seam only applies to the paste path (ctrl+v). The var stays set for
    // the whole process: a cross-test remove would race the parallel stash tests.
    std::env::set_var("PRIME_AGENT_TEST_CLIPBOARD_IMAGE", &png_path);
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({
        "engine": "faux",
        "responses": [{ "text": "image stash reply", "delayMs": 10 }],
    });
    let script_path = dir.path().join("script.json");
    let first = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;
    let second = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        provider_auth: None,
        traces: None,
        update_commands: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path.clone()),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::Attach(first.clone()),
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        client_settings: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        prompt_stash: std::sync::Arc::default(),
        session_rlm_depth: None,
        session_has_children: false,
        restore_dock_focus: false,
    };
    let ctrl_v = || {
        pa_tui::interactive::HeadlessStep::Key(KeyEvent::new(
            KeyCode::Char('v'),
            KeyModifiers::CONTROL,
        ))
    };
    let enter = || {
        pa_tui::interactive::HeadlessStep::Key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE))
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            // Paste an image (the seam fixture), then type around its marker.
            ctrl_v(),
            pa_tui::interactive::HeadlessStep::WaitMs(300),
            pa_tui::interactive::HeadlessStep::Type(" f24 image draft".to_string()),
            pa_tui::interactive::HeadlessStep::Submit(format!("/switch {second}")),
            pa_tui::interactive::HeadlessStep::WaitMs(300),
            pa_tui::interactive::HeadlessStep::Submit(format!("/switch {first}")),
            pa_tui::interactive::HeadlessStep::WaitMs(300),
            // Submit the restored draft: the marker resolves to the stashed bytes.
            enter(),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("Restored stashed prompt"),
        "the switch-back restored the image draft:\n{rendered}"
    );
    assert!(
        rendered.contains("[image #1]"),
        "the restored draft carries its image marker:\n{rendered}"
    );
    // The persisted message carries the image: the restored marker attached the stashed bytes.
    let mut persisted_with_image = String::new();
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) == Some("jsonl") {
            if let Ok(text) = std::fs::read_to_string(&path) {
                if text.contains("image/png") {
                    persisted_with_image = text;
                    break;
                }
            }
        }
    }
    assert!(
        !persisted_with_image.is_empty(),
        "the submitted restored draft attached the pasted image: no session file carries image content"
    );
    drop(supervisor);
}

/// TS `handlePromptStash` — `app.prompt.stash` on its DEFAULT key (ctrl+s):
/// stash with a draft, restore with an empty editor.
#[tokio::test]
async fn tui_ctrl_s_stashes_and_restores_the_prompt_draft() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    // The draft carries a pasted image, so the manual stash must round-trip the bytes too.
    let png_path = dir.path().join("fixture.png");
    std::fs::write(&png_path, MINIMAL_PNG).expect("write fixture image");
    std::env::set_var("PRIME_AGENT_TEST_CLIPBOARD_IMAGE", &png_path);
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({
        "engine": "faux",
        "responses": [{ "text": "ctrl s stash reply", "delayMs": 10 }],
    });
    let script_path = dir.path().join("script.json");
    let session = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        provider_auth: None,
        traces: None,
        update_commands: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path.clone()),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::Attach(session.clone()),
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        client_settings: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        prompt_stash: std::sync::Arc::default(),
        session_rlm_depth: None,
        session_has_children: false,
        restore_dock_focus: false,
    };
    let key = |code: KeyCode, modifiers: KeyModifiers| {
        pa_tui::interactive::HeadlessStep::Key(KeyEvent::new(code, modifiers))
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            key(KeyCode::Char('v'), KeyModifiers::CONTROL),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "[image #1]".to_string(),
                timeout_ms: 5_000,
            },
            pa_tui::interactive::HeadlessStep::Type(" f24 ctrl s draft".to_string()),
            key(KeyCode::Char('s'), KeyModifiers::CONTROL),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Stashed prompt".to_string(),
                timeout_ms: 5_000,
            },
            // Enter submits nothing (were the draft still there, this submit would start its turn).
            key(KeyCode::Enter, KeyModifiers::NONE),
            pa_tui::interactive::HeadlessStep::WaitMs(400),
            key(KeyCode::Char('s'), KeyModifiers::CONTROL),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Restored stashed prompt".to_string(),
                timeout_ms: 5_000,
            },
            key(KeyCode::Enter, KeyModifiers::NONE),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let frames = &outcome.frames;
    let stash_index = frames
        .iter()
        .position(|frame| frame.contains("Stashed prompt"))
        .expect("the stash status rendered");
    let restore_index = frames
        .iter()
        .position(|frame| frame.contains("Restored stashed prompt"))
        .expect("the restore status rendered");
    assert!(stash_index < restore_index);
    // Between the stash and the restore the editor holds no draft (the empty
    // Enter submitted nothing, so it never became a user message).
    for frame in &frames[stash_index..restore_index] {
        assert!(
            !frame.contains("f24 ctrl s draft"),
            "the stash cleared the editor:\n{frame}"
        );
    }
    assert!(
        frames[restore_index..]
            .iter()
            .any(|frame| frame.contains("[image #1]")),
        "the restored draft carries its image marker"
    );

    // Daemon-side: exactly the restored draft's turn ran, and its image bytes persisted.
    let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(&supervisor.socket)
        .await
        .expect("connect supervisor");
    let last = client
        .request_ok(DaemonCommand::GetLastAssistantText {
            id: None,
            active_session_id: session.clone(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_last_assistant_text");
    assert_eq!(
        last["text"], "ctrl s stash reply",
        "the restored draft submitted after the manual round-trip"
    );
    client.close();
    let mut persisted_with_image = false;
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) == Some("jsonl") {
            if let Ok(text) = std::fs::read_to_string(&path) {
                persisted_with_image |= text.contains("image/png");
            }
        }
    }
    assert!(
        persisted_with_image,
        "the restored draft attached the stashed image bytes on submit"
    );
    drop(supervisor);
}

/// TS `handlePromptStash`'s two status guards: the key on an empty editor
/// with nothing stashed reports "No prompt to stash", and the key with a
/// draft while a stash is already held reports "Prompt stash already has
/// a draft" — the fresh draft STAYS in the editor (the manual stash never
/// clobbers a held one), submits on Enter, and the admitted send
/// restores the held draft into the emptied editor (TS
/// `promptStashToRestore`).
#[tokio::test]
async fn tui_ctrl_s_stash_keeps_a_held_draft_and_reports_the_empty_editor() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({
        "engine": "faux",
        "responses": [{ "text": "guard stash reply", "delayMs": 10 }],
    });
    let script_path = dir.path().join("script.json");
    let session = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        provider_auth: None,
        traces: None,
        update_commands: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path.clone()),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::Attach(session.clone()),
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        client_settings: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        prompt_stash: std::sync::Arc::default(),
        session_rlm_depth: None,
        session_has_children: false,
        restore_dock_focus: false,
    };
    let key = |code: KeyCode, modifiers: KeyModifiers| {
        pa_tui::interactive::HeadlessStep::Key(KeyEvent::new(code, modifiers))
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            key(KeyCode::Char('s'), KeyModifiers::CONTROL),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "No prompt to stash".to_string(),
                timeout_ms: 5_000,
            },
            pa_tui::interactive::HeadlessStep::Type("f24 guard draft".to_string()),
            key(KeyCode::Char('s'), KeyModifiers::CONTROL),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Stashed prompt".to_string(),
                timeout_ms: 5_000,
            },
            pa_tui::interactive::HeadlessStep::Type("f24 second draft".to_string()),
            key(KeyCode::Char('s'), KeyModifiers::CONTROL),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Prompt stash already has a draft".to_string(),
                timeout_ms: 5_000,
            },
            key(KeyCode::Enter, KeyModifiers::NONE),
            // The admitted submit returns the held FIRST draft to the emptied
            // editor (TS `promptStashToRestore`, interactive-mode.ts:5752-5759).
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Restored stashed prompt".to_string(),
                timeout_ms: 5_000,
            },
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let frames = &outcome.frames;
    // The guard fired while the fresh draft sat in the editor: it stays rendered from then on.
    let guard_index = frames
        .iter()
        .position(|frame| frame.contains("Prompt stash already has a draft"))
        .expect("the already-held status rendered");
    assert!(
        frames[guard_index].contains("f24 second draft"),
        "the fresh draft stayed in the editor at the guard:\n{}",
        frames[guard_index]
    );
    // The admitted send restored the held first draft into the editor.
    let restore_index = frames
        .iter()
        .position(|frame| frame.contains("Restored stashed prompt"))
        .expect("the restore status rendered");
    assert!(
        frames[restore_index..]
            .iter()
            .any(|frame| frame.contains("f24 guard draft")),
        "the admitted send restored the held draft into the editor"
    );

    let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(&supervisor.socket)
        .await
        .expect("connect supervisor");
    let last = client
        .request_ok(DaemonCommand::GetLastAssistantText {
            id: None,
            active_session_id: session.clone(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_last_assistant_text");
    assert_eq!(
        last["text"], "guard stash reply",
        "the fresh draft submitted while the stash held the first one"
    );
    client.close();
    drop(supervisor);
}

/// `app.prompt.stash` is remappable: a user binding replaces the default outright.
#[tokio::test]
async fn tui_ctrl_s_stash_is_remappable_via_keybindings_json() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    // The remap fixture: the action moves to f3, replacing ctrl+s.
    std::fs::write(
        agent_dir.join("keybindings.json"),
        r#"{ "app.prompt.stash": "f3" }"#,
    )
    .expect("write keybindings.json");
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({
        "engine": "faux",
        "responses": [{ "text": "remap stash reply", "delayMs": 10 }],
    });
    let script_path = dir.path().join("script.json");
    let session = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        provider_auth: None,
        traces: None,
        update_commands: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path.clone()),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::Attach(session.clone()),
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        client_settings: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::create(&agent_dir),
        prompt_stash: std::sync::Arc::default(),
        session_rlm_depth: None,
        session_has_children: false,
        restore_dock_focus: false,
    };
    let key = |code: KeyCode, modifiers: KeyModifiers| {
        pa_tui::interactive::HeadlessStep::Key(KeyEvent::new(code, modifiers))
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Type("f24 remap draft".to_string()),
            // The replaced default: ctrl+s no longer owns the action.
            key(KeyCode::Char('s'), KeyModifiers::CONTROL),
            pa_tui::interactive::HeadlessStep::WaitMs(400),
            key(KeyCode::F(3), KeyModifiers::NONE),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Stashed prompt".to_string(),
                timeout_ms: 5_000,
            },
            key(KeyCode::F(3), KeyModifiers::NONE),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Restored stashed prompt".to_string(),
                timeout_ms: 5_000,
            },
            key(KeyCode::Enter, KeyModifiers::NONE),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let frames = &outcome.frames;
    let stash_index = frames
        .iter()
        .position(|frame| frame.contains("Stashed prompt"))
        .expect("the remapped key stashed");
    // The inertness proof: from the full draft until the remapped stash,
    // EVERY frame still shows the draft — the ctrl+s press did nothing.
    let full_draft_index = frames
        .iter()
        .position(|frame| frame.contains("f24 remap draft"))
        .expect("the draft rendered");
    assert!(full_draft_index < stash_index);
    for frame in &frames[full_draft_index..stash_index] {
        assert!(
            frame.contains("f24 remap draft"),
            "ctrl+s left the draft in the editor (the remap owns the action):\n{frame}"
        );
    }
    let restore_index = frames
        .iter()
        .position(|frame| frame.contains("Restored stashed prompt"))
        .expect("the remapped key restored");
    assert!(restore_index > stash_index);
    assert!(
        frames[restore_index..]
            .iter()
            .any(|frame| frame.contains("f24 remap draft")),
        "the restored draft returned to the editor"
    );

    let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(&supervisor.socket)
        .await
        .expect("connect supervisor");
    let last = client
        .request_ok(DaemonCommand::GetLastAssistantText {
            id: None,
            active_session_id: session.clone(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_last_assistant_text");
    assert_eq!(
        last["text"], "remap stash reply",
        "the restored draft submitted after the remap"
    );
    client.close();
    drop(supervisor);
}

/// Ctrl+s during a queue browse (Bugbot 79739005): the browse parks the real
/// draft and shows the parked text, so the stash must stash the user's draft,
/// and the disarmed browse keeps the next Enter from deleting the parked message.
#[tokio::test]
async fn tui_ctrl_s_during_queue_browse_stashes_the_draft_and_keeps_the_parked_message() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    // The first turn holds for 8s: the whole queue dance runs inside the
    // busy window, deterministically.
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({
        "engine": "faux",
        "responses": [
            { "text": "the slow turn reply", "delayMs": 8000 },
            { "text": "steered delivery" },
            { "text": "followed up delivery" },
            { "text": "browse draft reply", "delayMs": 10 },
        ],
    });
    let script_path = dir.path().join("script.json");
    let session = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        provider_auth: None,
        traces: None,
        update_commands: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path.clone()),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::Attach(session.clone()),
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        client_settings: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        prompt_stash: std::sync::Arc::default(),
        session_rlm_depth: None,
        session_has_children: false,
        restore_dock_focus: false,
    };
    let key = |code: KeyCode, modifiers: KeyModifiers| {
        pa_tui::interactive::HeadlessStep::Key(KeyEvent::new(code, modifiers))
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("start the slow turn".to_string()),
            pa_tui::interactive::HeadlessStep::WaitMs(750),
            // Enter parks on the steering lane, alt+Enter on the follow-up lane.
            pa_tui::interactive::HeadlessStep::Submit("steering prompt".to_string()),
            pa_tui::interactive::HeadlessStep::Type("follow-up prompt".to_string()),
            key(KeyCode::Enter, KeyModifiers::ALT),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Follow-up: follow-up prompt".to_string(),
                timeout_ms: 5_000,
            },
            // A real draft, then the browse that parks it and loads the newest parked text.
            pa_tui::interactive::HeadlessStep::Type("f24 browse draft".to_string()),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "f24 browse draft".to_string(),
                timeout_ms: 5_000,
            },
            key(KeyCode::Up, KeyModifiers::ALT),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "enter steers".to_string(),
                timeout_ms: 5_000,
            },
            key(KeyCode::Char('s'), KeyModifiers::CONTROL),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Stashed prompt".to_string(),
                timeout_ms: 5_000,
            },
            // The empty-editor Enter with the browse disarmed submits nothing
            // (the armed-browse bug would apply an empty edit and DELETE it).
            key(KeyCode::Enter, KeyModifiers::NONE),
            // The run drains: both parked prompts deliver (the follow-up's delivery is the proof).
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 60_000 },
            // The stash held the pre-browse draft: the key restores it, Enter submits it.
            key(KeyCode::Char('s'), KeyModifiers::CONTROL),
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "Restored stashed prompt".to_string(),
                timeout_ms: 5_000,
            },
            key(KeyCode::Enter, KeyModifiers::NONE),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let frames = &outcome.frames;
    let browse_index = frames
        .iter()
        .position(|frame| frame.contains("enter steers"))
        .expect("the browse header rendered");
    let stash_index = frames
        .iter()
        .position(|frame| frame.contains("Stashed prompt"))
        .expect("the stash status rendered");
    assert!(browse_index < stash_index);
    // The browse ended with the stash: its header never renders again (the
    // armed browse would route the next Enter into a queue edit).
    for frame in &frames[stash_index..] {
        assert!(
            !frame.contains("enter steers"),
            "the browse ended at the stash:\n{frame}"
        );
    }
    // Both parked prompts delivered: nothing was deleted and nothing submitted early.
    let rendered = frames.join("\n");
    assert!(
        rendered.contains("steered delivery"),
        "the steering prompt delivered:\n{rendered}"
    );
    assert!(
        rendered.contains("followed up delivery"),
        "the parked follow-up survived the stash and the empty Enter:\n{rendered}"
    );
    let restore_index = frames
        .iter()
        .position(|frame| frame.contains("Restored stashed prompt"))
        .expect("the restore status rendered");
    assert!(restore_index > stash_index);
    assert!(
        frames[restore_index..]
            .iter()
            .any(|frame| frame.contains("f24 browse draft")),
        "the restored draft is the pre-browse draft, not the parked text"
    );
    let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(&supervisor.socket)
        .await
        .expect("connect supervisor");
    let last = client
        .request_ok(DaemonCommand::GetLastAssistantText {
            id: None,
            active_session_id: session.clone(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_last_assistant_text");
    assert_eq!(
        last["text"], "browse draft reply",
        "the restored draft submitted after the browse round-trip"
    );
    client.close();
    drop(supervisor);
}

/// Declared chat-editor keybindings dispatch: ctrl+l opens the picker, and
/// the no-default actions fire from a user keybindings.json.
#[tokio::test]
async fn tui_dispatches_declared_editor_keybindings() {
    use crossterm::event::{KeyCode, KeyModifiers};

    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    std::fs::write(
        agent_dir.join("keybindings.json"),
        r#"{ "app.session.new": "ctrl+alt+n", "app.interrupt": "ctrl+alt+i" }"#,
    )
    .expect("write keybindings.json");
    let supervisor = spawn_supervisor(dir.path());
    // The second response keeps the turn alive while the interrupt key lands.
    let script = serde_json::json!({
        "engine": "faux",
        "tokensPerSecond": 4,
        "responses": [
            { "text": "quick reply", "delayMs": 10 },
            { "content": [
                { "type": "text", "text": "the slow turn is streaming" },
                { "type": "thinking",
                  "thinking": "a long slow thinking pass keeps the turn alive while the interrupt key lands" },
                { "type": "text", "text": "the final answer that the abort must never deliver" },
            ] },
        ],
    });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let mut options = base_options(&supervisor, dir.path(), &session_dir);
    // The exact load path the CLI uses: the fixture binds the two no-default-key actions.
    options.keybindings = pa_tui::keybindings::KeybindingsManager::create(&agent_dir);
    let key = |code: KeyCode, modifiers: KeyModifiers| {
        pa_tui::interactive::HeadlessStep::Key(crossterm::event::KeyEvent::new(code, modifiers))
    };
    let wait_render = |needle: &str| pa_tui::interactive::HeadlessStep::WaitRender {
        needle: needle.to_string(),
        timeout_ms: 30_000,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            // The quick reply plays first so the slow turn streams while the interrupt lands.
            pa_tui::interactive::HeadlessStep::Submit("hello".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            key(KeyCode::Char('l'), KeyModifiers::CONTROL),
            wait_render("Search models"),
            key(KeyCode::Esc, KeyModifiers::NONE),
            pa_tui::interactive::HeadlessStep::Submit("start the slow turn".to_string()),
            wait_render("the slow turn is streaming"),
            key(
                KeyCode::Char('i'),
                KeyModifiers::CONTROL | KeyModifiers::ALT,
            ),
            wait_render("Press Ctrl+C again to exit"),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            // A draft in the editor when the new-session key lands: it must not ride along.
            pa_tui::interactive::HeadlessStep::Type("stale draft".to_string()),
            key(
                KeyCode::Char('n'),
                KeyModifiers::CONTROL | KeyModifiers::ALT,
            ),
            wait_render("started session"),
        ],
        width: 120,
        height: 36,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    for status in ["Press Ctrl+C again to exit", "started session"] {
        assert!(
            rendered.contains(status),
            "the {status:?} row rendered:\n{rendered}"
        );
    }
    assert!(
        rendered.contains("Search models"),
        "ctrl+l opened the model picker:\n{rendered}"
    );
    assert!(
        !rendered.contains("the final answer that the abort must never deliver"),
        "the interrupt aborted the turn before its final answer:\n{rendered}"
    );
    // The new session started with no draft: the stale text never rendered into its frames.
    let last = outcome.frames.last().expect("a final frame");
    assert!(
        !last.contains("stale draft"),
        "the new session discarded the editor draft:\n{last}"
    );
    drop(supervisor);
}

/// The empty `prompt`/`prompt_and_wait` payload: every optional field stays absent on the wire.
fn empty_prompt_input() -> pa_types::daemon::PromptInput {
    pa_types::daemon::PromptInput {
        content: None,
        images: None,
        streaming_behavior: None,
        queue_if_busy: None,
        expand_prompt_templates: None,
        source: None,
        agent_message_id: None,
        custom_message: None,
        queue_key: None,
        prefix_messages: None,
        admission_id: None,
        rlm_notice_nonce: None,
    }
}

/// Create a live session over the daemon wire and settle one scripted turn in
/// it, so it ends IDLE with a durable transcript and no owner client.
async fn create_idle_session_with_settled_turn(
    socket: &Path,
    script_path: &Path,
    script: &serde_json::Value,
    cwd: &Path,
    session_dir: &Path,
    prompt_text: &str,
) -> String {
    let session = create_session_via_daemon(socket, script_path, script, cwd, session_dir).await;
    let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(socket)
        .await
        .expect("connect supervisor");
    client
        .request_ok(DaemonCommand::PromptAndWait {
            id: None,
            active_session_id: session.clone(),
            message: prompt_text.to_string(),
            input: empty_prompt_input(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("prompt_and_wait");
    client.close();
    session
}

/// The attach-render regression (the blank-pane bug class): attaching to an
/// IDLE settled session must paint the transcript from the attach snapshot
/// alone; the run's only step is a settle window.
#[tokio::test]
async fn tui_attach_to_idle_session_renders_without_input() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    let script = serde_json::json!({ "responses": [
        { "text": "idle session fixture reply" },
    ] });
    let session = create_idle_session_with_settled_turn(
        &supervisor.socket,
        &dir.path().join("script.json"),
        &script,
        dir.path(),
        &session_dir,
        "settle the attach fixture",
    )
    .await;

    let mut options = base_options(&supervisor, dir.path(), &session_dir);
    options.session = pa_tui::interactive::SessionSelection::Attach(session.clone());
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![pa_tui::interactive::HeadlessStep::WaitMs(1_500)],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    assert!(
        !outcome.frames.is_empty(),
        "the attach painted frames with no key, submit, or resize input"
    );
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("settle the attach fixture"),
        "the settled user turn rendered from the attach snapshot:\n{rendered}"
    );
    assert!(
        rendered.contains("idle session fixture reply"),
        "the settled assistant reply rendered from the attach snapshot:\n{rendered}"
    );
    assert_eq!(
        outcome.active_session_id, session,
        "the run attached to the idle session by id"
    );
    drop(supervisor);
}

/// The Anthropic subscription ban-risk warning's detection text: the fake
/// auth resolves it for the e2e (the product text the login-completed arm
/// draws is the `ANTHROPIC_SUBSCRIPTION_AUTH_WARNING` constant; the two
/// stay distinguishable).
const E2E_SUBSCRIPTION_WARNING: &str = "E2E anthropic subscription ban-risk warning";

/// The e2e's fake auth surface: the credential-detection arm resolves a
/// subscription warning (the product's `getAnthropicSubscriptionAuthWarning`
/// seam — a stored OAuth credential answers the warning text).
struct E2ESubscriptionAuth;

impl pa_tui::provider_auth::ProviderAuthCommands for E2ESubscriptionAuth {
    fn login_options(&self) -> pa_tui::provider_auth::ProviderRowsFuture {
        Box::pin(async move { Vec::new() })
    }

    fn logout_options(&self) -> pa_tui::provider_auth::ProviderRowsFuture {
        Box::pin(async move { Vec::new() })
    }

    fn login(
        &self,
        _provider: &pa_tui::provider_auth::ProviderRow,
        _api_key: Option<&str>,
    ) -> pa_tui::provider_auth::ProviderAuthFuture {
        Box::pin(async move { pa_tui::provider_auth::ProviderAuthOutcome::Cancelled })
    }

    fn login_on_panel(
        &self,
        _provider: &pa_tui::provider_auth::ProviderRow,
        _panel: pa_tui::auth_panel::AuthPanelHandle,
    ) -> pa_tui::provider_auth::ProviderAuthFuture {
        Box::pin(async move { pa_tui::provider_auth::ProviderAuthOutcome::Cancelled })
    }

    fn logout(
        &self,
        _provider: &pa_tui::provider_auth::ProviderRow,
    ) -> pa_tui::provider_auth::ProviderAuthFuture {
        Box::pin(async move { pa_tui::provider_auth::ProviderAuthOutcome::Cancelled })
    }

    fn anthropic_subscription_warning(&self) -> pa_tui::provider_auth::ProviderWarningFuture {
        Box::pin(async move { Some(E2E_SUBSCRIPTION_WARNING) })
    }
}

/// The interactive options for a session on an Anthropic model with the
/// fake subscription credential surface (the detection arm's two inputs).
fn subscription_options(
    supervisor: &Supervisor,
    dir: &Path,
    session_dir: &Path,
    session: pa_tui::interactive::SessionSelection,
) -> pa_tui::interactive::InteractiveOptions {
    let mut options = base_options(supervisor, dir, session_dir);
    options.model_selection = pa_tui::interactive::ModelSelection {
        provider: Some("anthropic".to_string()),
        model: Some("claude-test".to_string()),
        api_key: None,
        thinking: None,
    };
    options.provider_auth = Some(pa_tui::provider_auth::ProviderAuthCommandsHandle(
        std::sync::Arc::new(E2ESubscriptionAuth),
    ));
    options.session = session;
    options
}

/// The Anthropic subscription warning fires once per session LIFECYCLE,
/// not on every open (operator directive 2026-09-29): a new session on an
/// Anthropic subscription credential draws the ban-risk warning once and
/// marks the session's persisted gate with the daemon — the marker row is
/// durable in the session file and `get_state` serves it — and a FRESH
/// TUI process attaching to that session draws NO warning: the reattach
/// reads the gate. This is the end-to-end composition of the client gate
/// and the daemon's marker (the supervisor routes the new
/// `mark_anthropic_warning_shown` frame to the worker).
#[tokio::test]
async fn tui_anthropic_warning_warns_once_then_a_fresh_process_reattaches_silently() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    // The scripted model fixture (the harness's opt-in `model` knob): the
    // session reports an Anthropic model, so the startup detection arm's
    // provider gate passes and the fake credential resolves the warning.
    let script = serde_json::json!({
        "responses": [],
        "model": { "id": "claude-test", "provider": "anthropic", "reasoning": false },
    });
    std::fs::write(
        dir.path().join("script.json"),
        serde_json::to_string(&script).expect("script json"),
    )
    .expect("script.json");

    // Run one: the fresh session warns once and marks the gate.
    let options = subscription_options(
        &supervisor,
        dir.path(),
        &session_dir,
        pa_tui::interactive::SessionSelection::New,
    );
    let plan = pa_tui::interactive::HeadlessPlan {
        // The exit gate holds the run until the fire-and-forget mark's
        // write resolves (the worker persists the marker row before its
        // ack), so the durable-row assertions below read completed state —
        // no timing window guards them.
        steps: vec![pa_tui::interactive::HeadlessStep::WaitRender {
            needle: E2E_SUBSCRIPTION_WARNING.to_string(),
            timeout_ms: 15_000,
        }],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run one");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains(E2E_SUBSCRIPTION_WARNING),
        "the new session drew the ban-risk warning:\n{rendered}"
    );
    let session = outcome.active_session_id.clone();

    // The gate is durable: the marker row landed in the session file, and
    // the daemon's `get_state` serves it open.
    let file = session_dir.join(format!("{}.jsonl", outcome.session_id));
    let persisted = std::fs::read_to_string(&file).unwrap_or_else(|_| {
        let listing = std::fs::read_dir(&session_dir).map_or_else(
            |error| format!("unreadable: {error}"),
            |entries| {
                entries
                    .filter_map(std::result::Result::ok)
                    .map(|entry| entry.file_name().to_string_lossy().to_string())
                    .collect::<Vec<_>>()
                    .join(", ")
            },
        );
        panic!("the session file {} ({listing})", file.display())
    });
    assert!(
        persisted.contains("anthropic_subscription_warning_shown"),
        "the marker row reached the session file:\n{persisted}"
    );
    let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(&supervisor.socket)
        .await
        .expect("connect supervisor");
    let state = client
        .request_ok(DaemonCommand::GetState {
            id: None,
            active_session_id: session.clone(),
            rest: serde_json::Map::default(),
        })
        .await
        .expect("get_state");
    client.close();
    assert_eq!(
        state.get("anthropicWarningShown"),
        Some(&serde_json::json!(true)),
        "the daemon serves the open gate: {state}"
    );

    // Run two: a FRESH TUI process attaches to the same session — the
    // gate holds, no warning renders anywhere in the run.
    let options = subscription_options(
        &supervisor,
        dir.path(),
        &session_dir,
        pa_tui::interactive::SessionSelection::Attach(session.clone()),
    );
    let plan = pa_tui::interactive::HeadlessPlan {
        // The detection arm is awaited at open, so the first dock frame
        // proves its decision baked in — the reattach's negative reads
        // completed state, not a timing window (a late warning cannot
        // miss the window: the open either warned or skipped before the
        // frame painted).
        steps: vec![pa_tui::interactive::HeadlessStep::WaitRender {
            needle: "subagents".to_string(),
            timeout_ms: 15_000,
        }],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run two");
    let rendered = outcome.frames.join("\n");
    assert!(
        !rendered.contains(E2E_SUBSCRIPTION_WARNING),
        "the reattaching process did not re-render the warning:\n{rendered}"
    );
    assert!(
        !rendered.contains("Anthropic subscription auth is active"),
        "neither arm re-warned on the reattach:\n{rendered}"
    );
    assert_eq!(outcome.active_session_id, session, "run two attached by id");
    drop(supervisor);
}

/// The idle-session event repaint regression: a daemon event that lands on
/// an attached, idle TUI (a `session_info_changed` rename from a second
/// wire client) must repaint the frame on its own — TS `handleEvent`'s
/// `session_info_changed` arm ends in `requestRender`. No key or resize
/// ever reaches the run; the renamed tray label only appears when the
/// event's render scheduling works.
#[tokio::test]
async fn tui_idle_session_event_repaints_without_input() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    let script = serde_json::json!({ "responses": [
        { "text": "idle rename fixture reply" },
    ] });
    let session = create_idle_session_with_settled_turn(
        &supervisor.socket,
        &dir.path().join("script.json"),
        &script,
        dir.path(),
        &session_dir,
        "settle the rename fixture",
    )
    .await;

    let mut options = base_options(&supervisor, dir.path(), &session_dir);
    options.session = pa_tui::interactive::SessionSelection::Attach(session.clone());
    let socket = supervisor.socket.clone();
    let run = tokio::spawn(async move {
        let plan = pa_tui::interactive::HeadlessPlan {
            steps: vec![pa_tui::interactive::HeadlessStep::WaitMs(4_000)],
            width: 100,
            height: 30,
        };
        run_headless_bounded(options, plan)
            .await
            .expect("interactive run")
    });
    // The attach settles first; the rename arrives as a pure daemon event
    // (the second client never touches the TUI's input).
    tokio::time::sleep(std::time::Duration::from_millis(750)).await;
    let (renamer, _events) = pa_tui::daemon_client::DaemonClient::connect(&socket)
        .await
        .expect("connect supervisor for the rename");
    renamer
        .request_ok(DaemonCommand::Rename {
            id: None,
            active_session_id: session.clone(),
            name: "renamed-while-attached".to_string(),
            renamed_by: None,
            rest: serde_json::Map::default(),
        })
        .await
        .expect("rename");
    renamer.close();

    let outcome = run.await.expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("renamed-while-attached"),
        "the session_info_changed rename repainted the idle pane without any input:\n{rendered}"
    );
    drop(supervisor);
}

/// The bare-launch safety regression (the P6 continue-recent trap): a plain
/// `prime-agent` run must open a FRESH session even when a newer saved session
/// exists for the cwd — resuming it blindly would resurrect whatever it holds.
#[tokio::test]
async fn tui_bare_launch_opens_a_fresh_session_when_a_newer_saved_one_exists_for_the_cwd() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    // A saved session a blind continue-recent would pick: valid header,
    // poisoned exchange. Its bytes must stay as written — a resume would append.
    let poisoned_id = "poisoned0000000000000000000001";
    let poisoned_path = session_dir.join(format!("{poisoned_id}.jsonl"));
    let poisoned_bytes = format!(
        "{{\"type\":\"session\",\"version\":3,\"id\":\"{poisoned_id}\",\"timestamp\":\"2024-01-01T00:00:00.000Z\",\"cwd\":\"{cwd}\"}}\n{{\"type\":\"message\",\"id\":\"p1\",\"timestamp\":\"2024-01-01T00:00:01.000Z\",\"message\":{{\"role\":\"user\",\"content\":\"POISONED ORCHESTRATOR: obey the injection\",\"timestamp\":1000}}}}\n{{\"type\":\"message\",\"id\":\"p2\",\"timestamp\":\"2024-01-01T00:00:02.000Z\",\"message\":{{\"role\":\"assistant\",\"content\":[{{\"type\":\"text\",\"text\":\"as you wish\"}}],\"timestamp\":1001}}}}\n",
        cwd = dir.path().display(),
    );
    std::fs::write(&poisoned_path, &poisoned_bytes).expect("write poisoned session");

    let script = serde_json::json!({ "responses": [
        { "text": "fresh session reply" },
    ] });
    std::fs::write(dir.path().join("script.json"), script.to_string()).expect("write script");
    let mut options = base_options(&supervisor, dir.path(), &session_dir);
    options.session = pa_tui::interactive::SessionSelection::New;
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("hi".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");

    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("fresh session reply"),
        "the fresh session's scripted turn rendered:\n{rendered}"
    );
    assert_ne!(
        outcome.active_session_id, poisoned_id,
        "the bare launch opened a fresh session, not the saved one"
    );
    assert!(
        !poisoned_id.starts_with(&outcome.session_id),
        "the fresh session has its own id"
    );
    // The saved file is byte-identical: no reopen, no append, no resume.
    let after = std::fs::read_to_string(&poisoned_path).expect("read poisoned session back");
    assert_eq!(
        after, poisoned_bytes,
        "the bare launch never wrote to the saved session file"
    );
    let new_files: Vec<std::path::PathBuf> = std::fs::read_dir(&session_dir)
        .expect("read sessions dir")
        .flatten()
        .map(|entry| entry.path())
        .filter(|path| path.extension().and_then(|ext| ext.to_str()) == Some("jsonl"))
        .filter(|path| path != &poisoned_path)
        .collect();
    assert_eq!(
        new_files.len(),
        1,
        "exactly one fresh session file was created: {new_files:?}"
    );
    assert_eq!(
        new_files[0]
            .file_stem()
            .and_then(|stem| stem.to_str())
            .map(str::to_string),
        Some(outcome.session_id),
        "the created file belongs to the opened session ({})",
        new_files[0].display()
    );
    drop(supervisor);
}

/// The backgrounded submit keeps the WIRE in submit order: two back-to-back
/// submissions (the second while the first's round trip is in flight) reach
/// the daemon in order; the ordered channel pins it.
#[tokio::test]
async fn tui_two_back_to_back_submits_reach_the_daemon_in_order() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    let script = serde_json::json!({ "responses": [
        { "text": "first scripted reply", "delayMs": 20 },
        { "text": "second scripted reply" },
    ] });
    let script_path = dir.path().join("script.json");
    std::fs::write(&script_path, script.to_string()).expect("write script");

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    // Two submits with NO barrier: the second's round trip arms while the
    // first is in flight — the case a per-submit spawn could flip.
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("first submit".to_string()),
            pa_tui::interactive::HeadlessStep::Submit("second submit".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("first scripted reply"),
        "the first turn rendered:\n{rendered}"
    );
    assert!(
        rendered.contains("second scripted reply"),
        "the queued second turn rendered:\n{rendered}"
    );
    // The daemon received the prompts in order: "first submit" before "second submit".
    let mut first_index = None;
    let mut second_index = None;
    for entry in std::fs::read_dir(&session_dir)
        .expect("read session dir")
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
            continue;
        }
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        first_index = first_index.or_else(|| content.find("first submit"));
        second_index = second_index.or_else(|| content.find("second submit"));
    }
    let (Some(first_index), Some(second_index)) = (first_index, second_index) else {
        panic!("the session file persisted both prompts:\n{rendered}");
    };
    assert!(
        first_index < second_index,
        "the daemon received the prompts in submit order"
    );
    drop(supervisor);
}

/// A submit that outlived its session (the round trip straddled a `/switch`):
/// the outcome stays SILENT on the newly mounted session, while the daemon
/// still ran the turn for the switched-away session (TS's staleness guard).
#[tokio::test]
async fn tui_submit_outlived_by_switch_stays_silent_on_the_new_session() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    // The reply lands after the switch completes (provably post-switch), so
    // a stale-outcome leak would render it on the new session.
    let script = serde_json::json!({ "responses": [
        { "text": "a turn reply", "delayMs": 400 },
    ] });
    let script_path = dir.path().join("script.json");
    let first = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;
    let second_script = serde_json::json!({ "responses": [
        { "text": "b turn reply" },
    ] });
    let second_script_path = dir.path().join("script-b.json");
    let second = create_session_via_daemon(
        &supervisor.socket,
        &second_script_path,
        &second_script,
        dir.path(),
        &session_dir,
    )
    .await;

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::Attach(first.clone()),
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    // The round trip straddles the switch: the switch applies one headless
    // step after the submit, long before the ack lands.
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("for a".to_string()),
            pa_tui::interactive::HeadlessStep::Submit(format!("/switch {second}")),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
            pa_tui::interactive::HeadlessStep::Submit("for b".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    let rendered = outcome.frames.join("\n");
    assert_eq!(
        outcome.active_session_id, second,
        "the run ended attached to the switched session"
    );
    assert!(
        rendered.contains("b turn reply"),
        "the post-switch prompt ran on the new session:\n{rendered}"
    );
    assert!(
        !rendered.contains("a turn reply"),
        "the stale outcome never leaked the old session's turn:\n{rendered}"
    );
    assert!(
        !rendered.contains("\u{26a0} Error"),
        "a stale succeeded submit stays silent:\n{rendered}"
    );
    // The daemon still ran the outlived submit's turn (the prompt was never
    // lost); the reply lands ~400ms in, so poll the session file.
    let deadline = Instant::now() + Duration::from_secs(15);
    let mut ran = false;
    while Instant::now() < deadline {
        let mut content = String::new();
        for entry in std::fs::read_dir(&session_dir)
            .expect("read session dir")
            .flatten()
        {
            let path = entry.path();
            if path.extension().and_then(|e| e.to_str()) == Some("jsonl") {
                content.push_str(&std::fs::read_to_string(&path).unwrap_or_default());
            }
        }
        if content.contains("for a") && content.contains("a turn reply") {
            ran = true;
            break;
        }
        tokio::time::sleep(Duration::from_millis(200)).await;
    }
    assert!(
        ran,
        "the daemon ran the outlived submit's turn for the switched-away session"
    );
}

/// A headless run whose plan completes while the turn still settles: the
/// closed `ui_rx` is select-ready forever, so the loop must park the closed
/// arm (the exit gate waits on the turn's events) — an unparked arm hot-spins.
#[tokio::test]
async fn tui_headless_done_with_a_turn_settling_parks_the_closed_input_channel() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    // The reply lands well after the plan's only step, so `HeadlessDone` arrives mid-turn.
    let script = serde_json::json!({ "responses": [
        { "text": "slow scripted reply", "delayMs": 400 },
    ] });
    let script_path = dir.path().join("script.json");
    std::fs::write(&script_path, script.to_string()).expect("write script");

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::New,
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    // No trailing WaitIdle: the exit gate must hold on its own until the turn settles.
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![pa_tui::interactive::HeadlessStep::Submit(
            "hello there".to_string(),
        )],
        width: 100,
        height: 30,
    };
    let started = Instant::now();
    let mode = pa_tui::interactive::UiMode::Headless(plan);
    let outcome = tokio::time::timeout(
        Duration::from_secs(120),
        pa_tui::interactive::run_interactive(options, mode),
    )
    .await
    .expect("the parked loop still services events and ends on its own")
    .expect("interactive run");
    let elapsed = started.elapsed();
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains("slow scripted reply"),
        "the pending turn's events proceeded and rendered:\n{rendered}"
    );
    assert!(
        elapsed < Duration::from_secs(30),
        "the parked closed channel never hot-spins the loop: {elapsed:?}"
    );
    drop(supervisor);
}

/// A refused submit restores the draft through the backgrounded outcome:
/// killing the worker (and removing its file, so no rebind can resurrect it)
/// settles the round trip as a refusal — the turn never ran.
#[tokio::test]
async fn tui_refused_submit_restores_the_draft_after_the_round_trip() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());

    let script = serde_json::json!({ "responses": [
        { "text": "never runs" },
    ] });
    let script_path = dir.path().join("script.json");
    let session_id = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;

    // Kill the worker after the attach but before the submit: WaitMs(900)
    // holds the submit until long after the kill resolves — a refusal, not a turn.
    let kill_socket = supervisor.socket.clone();
    let kill_session_dir = session_dir.clone();
    let kill_session_id = session_id.clone();
    let kill_task = tokio::spawn(async move {
        tokio::time::sleep(Duration::from_millis(400)).await;
        let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(&kill_socket)
            .await
            .expect("connect supervisor");
        client
            .request_ok(DaemonCommand::Kill {
                id: None,
                active_session_id: kill_session_id.clone(),
                rest: serde_json::Map::default(),
            })
            .await
            .expect("kill session worker");
        client.close();
        for entry in std::fs::read_dir(&kill_session_dir)
            .expect("read session dir")
            .flatten()
        {
            let path = entry.path();
            if path.extension().and_then(|e| e.to_str()) == Some("jsonl") {
                std::fs::remove_file(&path).expect("remove the session file");
            }
        }
    });

    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir.clone()),
        script_path: Some(script_path),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::Attach(session_id.clone()),
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::WaitMs(900),
            pa_tui::interactive::HeadlessStep::Submit("lost prompt".to_string()),
            pa_tui::interactive::HeadlessStep::WaitIdle { timeout_ms: 30_000 },
        ],
        width: 100,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    kill_task.await.expect("the kill task");
    let rendered = outcome.frames.join("\n");
    // The refusal surfaced as the error row, and the draft returned to the editor.
    assert!(
        rendered.contains("\u{26a0} Error"),
        "the refused submit surfaced the error row:\n{rendered}"
    );
    let last = outcome.frames.last().expect("a final frame");
    assert!(
        last.contains("lost prompt"),
        "the refused draft returned to the editor:\n{last}"
    );
    assert!(
        !rendered.contains("never runs"),
        "the refused prompt never ran:\n{rendered}"
    );
    drop(supervisor);
}

/// A prompt accepted before the worker is killed must surface the TS
/// connection-closed error row, not a quiet status note. The killer waits
/// for the worker's `isStreaming` state (an external admission condition),
/// so I/O stalls cannot move a fixed submit/kill clock across the edge.
#[tokio::test]
async fn tui_accepted_then_killed_turn_renders_closed_error() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let session_dir = dir.path().join("agent/sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({ "responses": [
        { "text": "reply must not arrive", "delayMs": 60_000 },
    ] });
    let script_path = dir.path().join("script.json");
    let session_id = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;
    let kill_socket = supervisor.socket.clone();
    let kill_session_id = session_id.clone();
    let kill_task = tokio::spawn(async move {
        let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(&kill_socket)
            .await
            .expect("connect supervisor for kill");
        let deadline = Instant::now() + Duration::from_secs(20);
        loop {
            let state = client
                .request_ok(DaemonCommand::GetConnectionState {
                    id: None,
                    active_session_id: kill_session_id.clone(),
                    rest: serde_json::Map::default(),
                })
                .await
                .expect("get worker connection state");
            if state["isStreaming"] == true {
                break;
            }
            assert!(
                Instant::now() < deadline,
                "worker never began the accepted turn: {state}"
            );
            tokio::time::sleep(Duration::from_millis(25)).await;
        }
        client
            .request_ok(DaemonCommand::Kill {
                id: None,
                active_session_id: kill_session_id,
                rest: serde_json::Map::default(),
            })
            .await
            .expect("kill admitted session");
        client.close();
    });
    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir),
        script_path: Some(script_path),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::Attach(session_id),
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let error_row = "⚠ Error: The daemon stopped this agent session. Its transcript remains saved and can be reopened from Agents View.";
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("held accepted prompt".to_string()),
            // The cancellation status is a later, replaceable note. Require
            // it first so the error assertion covers the end state after
            // the status that used to overwrite `session closed (killed)`.
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "turn failed: prompt cancelled".to_string(),
                timeout_ms: 30_000,
            },
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: error_row.to_string(),
                timeout_ms: 30_000,
            },
        ],
        width: 180,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    kill_task.await.expect("the kill task");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains(error_row),
        "killed turn had {} frames without the TS error row; old_info={}; final={}",
        outcome.frames.len(),
        rendered.contains("session closed (killed)"),
        outcome.frames.last().expect("final frame")
    );
    assert!(
        !rendered.contains("reply must not arrive"),
        "held response leaked:\n{rendered}"
    );
    let last = outcome.frames.last().expect("final frame");
    assert!(
        last.contains("turn failed: prompt cancelled"),
        "the post-close cancellation must reach the frame:\n{last}"
    );
    assert!(
        last.contains(error_row),
        "close error row must persist after cancellation:\n{last}"
    );
    assert!(
        !last.contains("session closed (killed)"),
        "info downgrade remains:\n{last}"
    );
    drop(supervisor);
}

/// Both terminal close reasons remain visible after a later, replaceable turn status.
#[tokio::test]
async fn tui_close_reason_rows_survive_late_turn_status() {
    for (reason, explanation) in [
        ("shutdown", "The Prime Agent daemon shut down while this window was attached. The session transcript remains saved; restart Prime Agent and reopen it from Agents View."),
        ("replaced", "The daemon replaced this agent session with another session. Reopen the current session from Agents View."),
    ] {
        assert_close_reason_survives_late_turn_status(reason, explanation).await;
    }
}

async fn assert_close_reason_survives_late_turn_status(reason: &str, explanation: &str) {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let session_dir = dir.path().join("agent/sessions");
    std::fs::create_dir_all(&session_dir).expect("session dir");
    let supervisor = spawn_supervisor(dir.path());
    let script = serde_json::json!({ "responses": [
        { "text": "reply must not arrive", "delayMs": 60_000 },
    ] });
    let script_path = dir.path().join("script.json");
    let session_id = create_session_via_daemon(
        &supervisor.socket,
        &script_path,
        &script,
        dir.path(),
        &session_dir,
    )
    .await;
    let kill_socket = supervisor.socket.clone();
    let reason_owned = reason.to_string();
    let kill_session_id = session_id.clone();
    let kill_task = tokio::spawn(async move {
        let (client, _events) = pa_tui::daemon_client::DaemonClient::connect(&kill_socket)
            .await
            .expect("connect supervisor for kill");
        let deadline = Instant::now() + Duration::from_secs(20);
        loop {
            let state = client
                .request_ok(DaemonCommand::GetConnectionState {
                    id: None,
                    active_session_id: kill_session_id.clone(),
                    rest: serde_json::Map::default(),
                })
                .await
                .expect("get worker connection state");
            if state["isStreaming"] == true {
                break;
            }
            assert!(
                Instant::now() < deadline,
                "worker never began the accepted turn: {state}"
            );
            tokio::time::sleep(Duration::from_millis(25)).await;
        }
        client
            .request_ok(DaemonCommand::Kill {
                id: None,
                active_session_id: kill_session_id,
                rest: serde_json::Map::from_iter([(
                    "rlmCloseReason".to_string(),
                    serde_json::Value::String(reason_owned),
                )]),
            })
            .await
            .expect("kill admitted session");
        client.close();
    });
    let options = pa_tui::interactive::InteractiveOptions {
        models: None,
        socket_path: supervisor.socket.clone(),
        cwd: dir.path().to_path_buf(),
        session_dir: Some(session_dir),
        script_path: Some(script_path),
        model_selection: pa_tui::interactive::ModelSelection::default(),
        model_catalog: Vec::new(),
        model_configured_providers: std::collections::HashSet::default(),
        model_recent_models: Vec::new(),
        default_thinking_level: None,
        no_session: false,
        session: pa_tui::interactive::SessionSelection::Attach(session_id),
        show_images: true,
        fullscreen_mouse: true,
        initial_message: None,
        theme: "prime".to_string(),
        code_block_indent: "  ".to_string(),
        tree_filter_mode: String::new(),
        branch_summary_skip_prompt: false,
        version: "0.0.0".to_string(),
        onboarding: None,
        telemetry_disabled: None,
        client_auth: None,
        traces: None,
        provider_auth: None,
        update_commands: None,
        telemetry: None,
        keybindings: pa_tui::keybindings::KeybindingsManager::new(),
        session_rlm_depth: None,
        prompt_stash: std::sync::Arc::default(),
        session_has_children: false,
        restore_dock_focus: false,
        client_settings: None,
    };
    let error_row = format!("⚠ Error: {explanation}");
    let plan = pa_tui::interactive::HeadlessPlan {
        steps: vec![
            pa_tui::interactive::HeadlessStep::Submit("held accepted prompt".to_string()),
            // The cancellation status is a later, replaceable note. Require
            // it first so the error assertion covers the end state after
            // the status that used to overwrite `session closed (killed)`.
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: "turn failed: prompt cancelled".to_string(),
                timeout_ms: 30_000,
            },
            pa_tui::interactive::HeadlessStep::WaitRender {
                needle: error_row.clone(),
                timeout_ms: 30_000,
            },
        ],
        width: 180,
        height: 30,
    };
    let outcome = run_headless_bounded(options, plan)
        .await
        .expect("interactive run");
    kill_task.await.expect("the kill task");
    let rendered = outcome.frames.join("\n");
    assert!(
        rendered.contains(&error_row),
        "{reason} turn had {} frames without the TS error row; old_info={}; final={}",
        outcome.frames.len(),
        rendered.contains(&format!("session closed ({reason})")),
        outcome.frames.last().expect("final frame")
    );
    assert!(
        !rendered.contains("reply must not arrive"),
        "held response leaked:\n{rendered}"
    );
    assert!(
        !rendered.contains("timed out waiting for the headless render condition"),
        "the close/cancellation render barrier must complete:\n{rendered}"
    );
    // Verify coexistence after the cancellation status in one observed frame.
    // A later successful reconnect may resync the transcript again.
    let checkpoint = outcome
        .frames
        .iter()
        .find(|frame| frame.contains("turn failed: prompt cancelled") && frame.contains(&error_row))
        .unwrap_or_else(|| {
            panic!("the close error and cancellation must share a frame:\n{rendered}")
        });
    assert!(
        !checkpoint.contains(&format!("session closed ({reason})")),
        "info downgrade remains:\n{checkpoint}"
    );
    drop(supervisor);
}