Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

36.4 KB
Raw Blame
//! One browser page per job (0.9.3). A team's bots shared one page, so a sign-in the person was
//! asked to do on it (a held page) stopped every other job of the team. Now a bot turn in a chat
//! gets its own page, keyed by (sandbox target, bot, chat): a new page in the team's existing
//! browser context, so sign-ins and cookies are shared, while a hold for the person's step holds
//! only the page it was asked on. The person's panel and calls that name no chat keep the team
//! page, whose key is the sandbox target itself.
//!
//! Limits: at most [`MAX_JOB_PAGES_PER_TEAM`] job pages per team (the least recently used idle
//! one is closed to make room) and a job page nobody used for [`JOB_PAGE_IDLE`] is closed. A
//! page with an open step, one the person is driving, or one whose job (bot and chat) still has
//! a turn running is never closed. Room is made and a page opened under one lock per team
//! ([`JobPages::team_lock`]), so jobs opening at once never pass the cap. A bot tool that finds
//! no room waits up to [`PAGE_WAIT`] for a page to free up, then gets [`pages_full_message`].
//!
//! Resting and recovery (0.9.5). In VM mode a page no bot has used and nobody has watched for
//! [`IDLE_FREEZE`] is put to rest: its media paused and the page frozen, so a feed left
//! autoplaying doesn't burn the browser's CPU and memory; a team page idle for [`IDLE_BLANK`] is
//! sent to about:blank (its cookies stay in the jar). A rested page wakes before its next
//! action, screenshot or live view. When the browser stops answering, a bot's call retries with
//! backoff for up to [`RETRY_WINDOW`], first freeing memory by closing or blanking other idle
//! pages ([`relieve`]), never the page the bot is using; if it still can't, the bot is told the
//! problem is temporary ([`crate::sandbox::service::BROWSER_BUSY`]).
//!
//! Closing idle connections (#753). A page's controller keeps a DevTools connection open for as
//! long as it's cached: one for the page itself, plus a download/chooser connection and a
//! browser-level one each for its site guard and popup watcher. Freezing or blanking a page
//! doesn't touch any of these, and before this fix nothing ever did for a page that stayed open
//! (every team page, and a job page short of [`JOB_PAGE_IDLE`]) or even reliably for one that
//! was closed: on a long-running kwant they piled up until the browser ran out of the OS
//! resources (processes, threads) they each cost and every `browser_open` failed. A page idle
//! past [`CONN_IDLE_CLOSE`] now has its connections closed ([`close_idle_connections`]); its
//! sign-ins and place in the page stay, and its next use reconnects lazily
//! ([`crate::sandbox::service::BrowserService::open_session`]). Never a page that's watched, held
//! for a person, or mid-download or -chooser ([`connections_closable`]).
//!
//! The browser's process budget (31b). kwant's browser container can't fork past
//! [`BROWSER_PIDS_LIMIT`] processes and threads, and every open page holds a share of them (its
//! renderer, its context's network and storage, and one DevTools proxy process per connection).
//! Measured on kwnt-browser:v5: about 116 idle, 30-54 for a team page (its new context) and 13-32
//! for a job page, so three busy teams' 15 pages sat at about 444 and 17 at 496. Past the limit
//! Chrome can't start a page's processes and the browser fails in ways that look unrelated (a
//! download that never ends, `net::ERR_EMPTY_RESPONSE`, BROWSER_BUSY). So before a new job page
//! opens, the pages of every team are kept under [`PAGE_PID_BUDGET`] ([`pids_with`]): the least
//! recently used idle job page of any team is closed to make room, and when none can close the
//! job is told plainly that the browser is full ([`browser_full_message`]). Freezing or blanking
//! a page frees none of its processes (blanking briefly adds one), so only closing makes room. A
//! team page is never refused or closed; it counts, so job pages give way to it.

use super::continuity::SelfScope;
use super::AppState;
use crate::sandbox::service::{Rest, SeenLocation};
use axum::{extract::{Path, State}, Json};
use serde_json::{json, Value};
use std::{
    collections::HashMap,
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
    },
    time::{Duration, Instant},
};

pub use crate::sandbox::service::{is_job_page, job_page_key, page_team, PAGE_SEP};

pub use crate::sandbox::service::BROWSER_PIDS_LIMIT;
/// The idle browser's own processes and threads, with no page open (measured 105-116).
pub const BROWSER_IDLE_PIDS: u64 = 120;
/// Kept free for what isn't a page: downloads copied out with `docker exec`, a live view's
/// connection, a blanked page's short-lived renderer (+11 measured).
pub const PIDS_HEADROOM: u64 = 64;
/// A team page: the first page of a team's browser context (measured 30-54 with its connections).
pub const TEAM_PAGE_PIDS: u64 = 40;
/// A job page: one more page in its team's context (measured 13-32 with its connections, about 19
/// on average).
pub const JOB_PAGE_PIDS: u64 = 20;
/// What every open page of the browser together may hold: room for three teams' pages and ten job
/// pages, or two teams with all eight of theirs.
pub const PAGE_PID_BUDGET: u64 = BROWSER_PIDS_LIMIT - BROWSER_IDLE_PIDS - PIDS_HEADROOM;

/// What one open page holds of the budget.
pub fn page_pids(key: &str) -> u64 {
    if is_job_page(key) {
        JOB_PAGE_PIDS
    } else {
        TEAM_PAGE_PIDS
    }
}

/// What the browser's pages would hold with `key` open beside the `open` ones: a job page whose
/// team has no page open yet opens its team's page (the context) too.
pub fn pids_with(open: &[String], key: &str) -> u64 {
    let others: u64 = open.iter().filter(|k| *k != key).map(|k| page_pids(k)).sum();
    let team = page_team(key);
    let team_too = is_job_page(key) && !open.iter().any(|k| k == team);
    others + page_pids(key) + if team_too { TEAM_PAGE_PIDS } else { 0 }
}

/// What a job is told when the browser holds as many pages as it can run (its process budget)
/// and every one is held or in use.
pub fn browser_full_message() -> String {
    "The browser is full: it has as many pages open as it can run, and every one is in use by a \
     job or waiting for a person. Try again in a minute, or finish a job that has a page open; \
     nothing is wrong with the browser."
        .to_string()
}

/// Most job pages one team keeps open at once.
pub const MAX_JOB_PAGES_PER_TEAM: usize = 4;
/// A job page unused this long is closed (unless a step holds it).
pub const JOB_PAGE_IDLE: Duration = Duration::from_secs(30 * 60);
/// How often idle job pages are looked for.
pub const SWEEP_EVERY: Duration = Duration::from_secs(60);
/// How long a bot's browser call waits for one of its team's pages to free up before it is
/// told every page is in use.
pub const PAGE_WAIT: Duration = Duration::from_secs(60);
/// VM mode: a page no bot used and nobody watched for this long is frozen (media paused).
pub const IDLE_FREEZE: Duration = Duration::from_secs(10 * 60);
/// VM mode: a team page idle this long is sent to about:blank.
pub const IDLE_BLANK: Duration = Duration::from_secs(2 * 60 * 60);
/// VM mode: a page idle this long has its DevTools connections closed (#753): the download, site
/// guard and popups tasks that each hold one of their own, same as [`IDLE_FREEZE`] since a frozen
/// page has nothing left for them to watch. The page and its sign-ins stay; the next use
/// reconnects lazily. Never one that's watched, held for a person, or has a download or file
/// chooser in flight.
pub const CONN_IDLE_CLOSE: Duration = IDLE_FREEZE;
/// How long a bot's browser call keeps retrying a browser that doesn't answer (VM mode).
pub const RETRY_WINDOW: Duration = Duration::from_secs(45);
/// The first wait between retries; each next one doubles, up to [`RETRY_MAX_DELAY`].
pub const RETRY_FIRST_DELAY: Duration = Duration::from_secs(1);
pub const RETRY_MAX_DELAY: Duration = Duration::from_secs(8);
/// Freeing memory for a stalled browser leaves alone a page used this recently.
const RELIEF_IDLE: Duration = Duration::from_secs(60);
/// Longest one rest or relief step on one page may take (the browser may be stalled).
const REST_STEP: Duration = Duration::from_secs(5);
/// Longest all of one [`relieve`] may take.
const RELIEF_BUDGET: Duration = Duration::from_secs(15);

/// The wait before retry number `attempt` (0-based): 1, 2, 4, 8, 8, ... seconds.
pub fn retry_delay(attempt: u32) -> Duration {
    RETRY_FIRST_DELAY.saturating_mul(1u32 << attempt.min(16)).min(RETRY_MAX_DELAY)
}

/// Whether to try once more after waiting `delay`, `elapsed` into a retry `window`: only while
/// the next attempt still starts inside it.
pub fn keep_retrying(elapsed: Duration, delay: Duration, window: Duration) -> bool {
    elapsed + delay < window
}

/// What a job is told when every job page of its team is in use and none freed up in time.
pub fn pages_full_message() -> String {
    format!(
        "All {MAX_JOB_PAGES_PER_TEAM} of this team's browser pages are in use by other jobs. \
         Try again in a minute; nothing is wrong with the browser."
    )
}

/// Opening job pages: one lock per team, so making room and opening the page happen as one
/// step, and how long a bot call waits for room.
#[derive(Default)]
pub struct JobPages {
    opening: std::sync::Mutex<HashMap<String, Arc<tokio::sync::Mutex<()>>>>,
    /// Tests only: the wait for room in ms (0 = [`PAGE_WAIT`]).
    wait_ms: AtomicU64,
    /// Tests only: the retry window in ms (0 = [`RETRY_WINDOW`]).
    retry_ms: AtomicU64,
    /// Tests only: the page budget (0 = [`PAGE_PID_BUDGET`]).
    budget: AtomicU64,
    /// Held, after a team's lock, while room is made in the whole browser and a page opened, so
    /// two teams opening at once never pass [`PAGE_PID_BUDGET`] together.
    room: Arc<tokio::sync::Mutex<()>>,
}

impl JobPages {
    /// The lock held while a page of `team` is made room for and opened, or closed.
    pub fn team_lock(&self, team: &str) -> Arc<tokio::sync::Mutex<()>> {
        self.opening.lock().unwrap_or_else(|e| e.into_inner()).entry(team.to_string()).or_default().clone()
    }

    /// Tests: a shorter wait for room than [`PAGE_WAIT`].
    pub fn set_wait(&self, wait: Duration) {
        self.wait_ms.store((wait.as_millis() as u64).max(1), Ordering::Relaxed);
    }

    pub fn wait(&self) -> Duration {
        match self.wait_ms.load(Ordering::Relaxed) {
            0 => PAGE_WAIT,
            ms => Duration::from_millis(ms),
        }
    }

    /// Tests: a shorter retry window than [`RETRY_WINDOW`].
    pub fn set_retry_window(&self, window: Duration) {
        self.retry_ms.store((window.as_millis() as u64).max(1), Ordering::Relaxed);
    }

    pub fn retry_window(&self) -> Duration {
        match self.retry_ms.load(Ordering::Relaxed) {
            0 => RETRY_WINDOW,
            ms => Duration::from_millis(ms),
        }
    }

    /// Tests: a smaller page budget than [`PAGE_PID_BUDGET`].
    pub fn set_page_budget(&self, pids: u64) {
        self.budget.store(pids.max(1), Ordering::Relaxed);
    }

    pub fn page_budget(&self) -> u64 {
        match self.budget.load(Ordering::Relaxed) {
            0 => PAGE_PID_BUDGET,
            pids => pids,
        }
    }

    /// The lock held while room is made in the whole browser (see [`JobPages::room`]).
    pub fn room_lock(&self) -> Arc<tokio::sync::Mutex<()>> {
        self.room.clone()
    }
}

/// A job page's team, bot and chat; `None` for the team page.
pub fn job_of(key: &str) -> Option<(&str, &str, &str)> {
    let mut parts = key.splitn(3, PAGE_SEP);
    match (parts.next(), parts.next(), parts.next()) {
        (Some(team), Some(kwant), Some(chat)) => Some((team, kwant, chat)),
        _ => None,
    }
}

/// The page a turn's browser calls use: the job's own page when the turn runs in a chat
/// ([`super::human_steps::chat_key`], the same chat the bot's browser address names), else the
/// team page. `None` when the turn has no bot browser.
pub fn scope_page(scope: &SelfScope) -> Option<String> {
    let team = scope.sandbox_target.as_deref()?;
    Some(
        super::human_steps::chat_key(scope)
            .and_then(|chat| job_page_key(team, &scope.kwant_id, &chat))
            .unwrap_or_else(|| team.to_string()),
    )
}

/// The page a call on the bot browser API uses: the chat route's job page, else (the plain route,
/// or a bot route without a chat) the team page.
pub fn call_page(team: &str, kwant: Option<&str>, chat: Option<&str>) -> String {
    match (kwant, chat) {
        (Some(kwant), Some(chat)) => job_page_key(team, kwant, chat).unwrap_or_else(|| team.to_string()),
        _ => team.to_string(),
    }
}

/// Whether a page may be closed now: no open step holds it, its job has no turn still running
/// (a bot waiting minutes on a render between two calls comes back to its page, not a blank
/// one), and nobody (the person, or a bot's action) is using it.
async fn closable(state: &AppState, key: &str) -> bool {
    if state.db.human_step_open_on(key).await.unwrap_or(true) {
        return false;
    }
    if let Some((team, kwant, chat)) = job_of(key) {
        if state.human_steps.has_turn_in(team, kwant, chat) {
            return false;
        }
    }
    match state.browser_controllers.lock().await.get(key) {
        Some(ctrl) => ctrl.control.try_write().is_ok() && !ctrl.is_held_for_person(),
        None => true,
    }
}

/// Whether a page may be put to rest now: [`closable`], and no live view shows it.
async fn restable(state: &AppState, key: &str) -> bool {
    let watched = state.browser_controllers.lock().await.get(key).is_some_and(|c| c.watchers() > 0);
    !watched && closable(state, key).await
}

/// Puts one page to rest (see [`Rest`]) through its controller, or one made for the open page
/// when no bot has used it this run. Bounded by [`REST_STEP`]. Whether it was.
async fn rest_page(state: &AppState, key: &str, rest: Rest) -> bool {
    let ctrl = state.browser_controllers.lock().await.get(key).cloned();
    let ctrl = match ctrl {
        Some(ctrl) => ctrl,
        None => match state.browser_service.existing_page(key).await {
            Some(ctrl) => ctrl,
            None => return false,
        },
    };
    let done = match rest {
        Rest::Frozen => tokio::time::timeout(REST_STEP, ctrl.freeze()).await,
        Rest::Blank => tokio::time::timeout(REST_STEP, ctrl.blank()).await,
    };
    match done {
        Ok(Ok(())) => {
            state.browser_service.mark_rested(key, rest);
            true
        }
        Ok(Err(e)) => {
            tracing::debug!(page = %key, "couldn't put an idle page to rest: {e}");
            false
        }
        Err(_) => {
            tracing::debug!(page = %key, "putting an idle page to rest timed out");
            false
        }
    }
}

/// What the idle sweep does with a page idle for `idle` that was last put to rest as `rested`:
/// nothing before `freeze_after`; a team page past `blank_after` is blanked; any other is
/// frozen. `None` when it already is.
pub fn rest_for(job: bool, idle: Duration, rested: Option<Rest>, freeze_after: Duration, blank_after: Duration) -> Option<Rest> {
    if idle < freeze_after || rested == Some(Rest::Blank) {
        return None;
    }
    let want = if !job && idle >= blank_after { Rest::Blank } else { Rest::Frozen };
    (rested != Some(want)).then_some(want)
}

/// VM mode: puts to rest the pages idle past `freeze_after` (see [`rest_for`]) that no step
/// holds, no turn of their job is running in, and nobody is using or watching. Returns how many
/// were (frozen, blanked).
pub async fn rest_idle(state: &AppState, freeze_after: Duration, blank_after: Duration) -> (usize, usize) {
    if state.browser_service.mode() == crate::sandbox::computer::ComputerMode::Local {
        return (0, 0);
    }
    let now = Instant::now();
    let (mut frozen, mut blanked) = (0, 0);
    for (key, at) in state.browser_service.open_pages().await {
        let rested = state.browser_service.rest_of(&key);
        let Some(rest) = rest_for(is_job_page(&key), now.duration_since(at), rested, freeze_after, blank_after) else { continue };
        if !restable(state, &key).await || !rest_page(state, &key, rest).await {
            continue;
        }
        tracing::info!(page = %key, ?rest, "browser: put an idle page to rest");
        match rest {
            Rest::Frozen => frozen += 1,
            Rest::Blank => blanked += 1,
        }
    }
    (frozen, blanked)
}

/// Whether a page's connections may be closed given whether it's [`restable`] and has a download
/// or file chooser in flight: closing mid-download would drop it, and a chooser the bot hasn't
/// answered yet would be lost.
pub fn connection_closable(restable: bool, downloading: bool, chooser_open: bool) -> bool {
    restable && !downloading && !chooser_open
}

/// Whether a page's connections may be closed now: see [`connection_closable`].
async fn connections_closable(state: &AppState, key: &str) -> bool {
    let restable = restable(state, key).await;
    if !restable {
        return false;
    }
    let ctrl = state.browser_controllers.lock().await.get(key).cloned();
    let Some(ctrl) = ctrl else { return true };
    let downloading = ctrl.peek_download_report().is_some_and(|r| r["status"] == "in_progress");
    connection_closable(restable, downloading, ctrl.open_chooser_report().is_some())
}

/// Closes one page's DevTools connections (its controller's download, site guard and popups
/// tasks) without closing the page itself or its context: the next use reconnects lazily and its
/// sign-ins stay (`persist_cookies` first, belt and suspenders for a VM container restart; the
/// browser's own cookie store for the page's context is untouched either way). Whether it was.
async fn close_connection(state: &AppState, key: &str) -> bool {
    let ctrl = state.browser_controllers.lock().await.remove(key);
    if let Some(ctrl) = &ctrl {
        ctrl.persist_cookies().await;
    }
    let dropped_cached = state.browser_service.drop_connection(key).await;
    ctrl.is_some() || dropped_cached
}

/// VM mode: closes the DevTools connections of pages idle past [`CONN_IDLE_CLOSE`] (job pages
/// not yet past [`JOB_PAGE_IDLE`], and team pages, which [`sweep_idle`] never closes at all).
/// Returns how many.
pub async fn close_idle_connections(state: &AppState, after: Duration) -> usize {
    if state.browser_service.mode() == crate::sandbox::computer::ComputerMode::Local {
        return 0;
    }
    let now = Instant::now();
    let mut closed = 0;
    for (key, at) in state.browser_service.open_pages().await {
        if now.duration_since(at) < after || !connections_closable(state, &key).await {
            continue;
        }
        if close_connection(state, &key).await {
            closed += 1;
            tracing::info!(page = %key, "browser: closed an idle page's connections");
        }
    }
    closed
}

/// The browser stopped answering a call for `keep`: frees memory and DevTools connections by
/// closing other idle job pages and blanking and disconnecting other idle team pages, least
/// recently used first, never `keep` nor a page used in the last minute, watched, held, mid-
/// download, or otherwise in use. A `BROWSER_BUSY` the resource-exhaustion fix (#753) couldn't
/// prevent is exactly this: too many of these connections open at once, so relief drops them too,
/// not just memory, before a retry gives up. Each step is bounded (the browser may be stalled),
/// and all of it by [`RELIEF_BUDGET`]. Returns how many pages were closed, blanked or disconnected.
pub async fn relieve(state: &AppState, keep: &str) -> usize {
    if state.browser_service.mode() == crate::sandbox::computer::ComputerMode::Local {
        return 0;
    }
    let mut relieved = 0;
    let work = async {
        let now = Instant::now();
        let pages = match tokio::time::timeout(REST_STEP, state.browser_service.open_pages()).await {
            Ok(pages) => pages,
            Err(_) => return,
        };
        for (key, at) in pages {
            if key == keep || now.duration_since(at) < RELIEF_IDLE || !restable(state, &key).await {
                continue;
            }
            if is_job_page(&key) {
                // a page being opened or closed for its team is left to that
                let lock = state.job_pages.team_lock(page_team(&key));
                let Ok(_opening) = lock.try_lock() else { continue };
                if tokio::time::timeout(REST_STEP, close_page(state, &key)).await.is_ok() {
                    relieved += 1;
                }
                continue;
            }
            let blanked = state.browser_service.rest_of(&key) != Some(Rest::Blank) && rest_page(state, &key, Rest::Blank).await;
            let disconnected = connections_closable(state, &key).await && close_connection(state, &key).await;
            if blanked || disconnected {
                relieved += 1;
            }
        }
    };
    let _ = tokio::time::timeout(RELIEF_BUDGET, work).await;
    if relieved > 0 {
        tracing::warn!(page = %keep, "browser: not answering; closed, blanked or disconnected {relieved} other idle pages to free resources");
    }
    relieved
}

/// Closes a job page: its controller first (cookies kept), then the page.
pub async fn close_page(state: &AppState, key: &str) {
    let ctrl = state.browser_controllers.lock().await.remove(key);
    if let Some(ctrl) = ctrl {
        ctrl.persist_cookies().await;
    }
    if let Err(e) = state.browser_service.close_page(key).await {
        tracing::debug!("couldn't close the job page {key}: {e}");
    }
    tracing::info!(page = %key, "closed a job's browser page");
}

/// Before a new job page opens in `team`: makes room under [`MAX_JOB_PAGES_PER_TEAM`] by closing
/// the least recently used page that can be closed. `false` when every page is held or in use.
/// The caller holds the team's [`JobPages::team_lock`] until the new page is open.
pub async fn make_room(state: &AppState, team: &str, key: &str) -> bool {
    let pages = state.browser_service.job_pages(team).await;
    let open: Vec<_> = pages.into_iter().filter(|(k, _)| k != key).collect();
    if open.len() < MAX_JOB_PAGES_PER_TEAM {
        return true;
    }
    let mut to_close = open.len() + 1 - MAX_JOB_PAGES_PER_TEAM;
    for (page, _) in open {
        if to_close == 0 {
            break;
        }
        if closable(state, &page).await {
            close_page(state, &page).await;
            to_close -= 1;
        }
    }
    to_close == 0
}

/// 31b: before a new job page `key` opens: keeps every team's open pages under the browser's
/// process budget ([`JobPages::page_budget`], [`pids_with`]) by closing the least recently used
/// job pages of any team that can be closed ([`closable`]). Never a team page or `key`; another
/// team's page only when its team's lock is free (a page that team is opening or closing is left
/// to it). `false` when the budget can't be met. Local mode (the person's own Chrome) has no such
/// limit. The caller holds its team's [`JobPages::team_lock`] and then [`JobPages::room_lock`]
/// until the new page is open.
pub async fn make_browser_room(state: &AppState, key: &str) -> bool {
    if state.browser_service.mode() == crate::sandbox::computer::ComputerMode::Local {
        return true;
    }
    let budget = state.job_pages.page_budget();
    let pages = state.browser_service.live_pages().await;
    let mut open: Vec<String> = pages.iter().map(|(k, _)| k.clone()).collect();
    if pids_with(&open, key) <= budget {
        return true;
    }
    let team = page_team(key);
    for (page, _) in pages {
        if page == key || !is_job_page(&page) {
            continue;
        }
        let other_team = (page_team(&page) != team).then(|| state.job_pages.team_lock(page_team(&page)));
        let _other_team_lock = match &other_team {
            Some(lock) => match lock.try_lock() {
                Ok(guard) => Some(guard),
                Err(_) => continue,
            },
            None => None,
        };
        if !closable(state, &page).await {
            continue;
        }
        close_page(state, &page).await;
        open.retain(|k| *k != page);
        tracing::info!(page = %page, for_page = %key, "browser: closed the least recently used idle job page to stay under the browser's process limit");
        if pids_with(&open, key) <= budget {
            return true;
        }
    }
    tracing::warn!(page = %key, "the browser is at its page budget and every job page is held or in use");
    false
}

/// Closes job pages unused for `idle` that no step holds and nobody is using. Returns how many.
pub async fn sweep_idle(state: &AppState, idle: Duration) -> usize {
    let now = Instant::now();
    let teams: std::collections::BTreeSet<String> = state
        .browser_service
        .pages_in_use()
        .into_iter()
        .filter(|(k, _)| is_job_page(k))
        .map(|(k, _)| page_team(&k).to_string())
        .collect();
    let mut closed = 0;
    for team in teams {
        let lock = state.job_pages.team_lock(&team);
        let _opening = lock.lock().await;
        for (page, at) in state.browser_service.job_pages(&team).await {
            if now.duration_since(at) >= idle && closable(state, &page).await {
                close_page(state, &page).await;
                closed += 1;
            }
        }
    }
    closed
}

/// Runs [`sweep_idle`] and [`rest_idle`] every [`SWEEP_EVERY`] until `cancel`.
pub fn spawn_sweeper(state: AppState, cancel: tokio_util::sync::CancellationToken) {
    tokio::spawn(async move {
        loop {
            tokio::select! {
                _ = cancel.cancelled() => return,
                _ = tokio::time::sleep(SWEEP_EVERY) => {}
            }
            let closed = sweep_idle(&state, JOB_PAGE_IDLE).await;
            if closed > 0 {
                tracing::info!("browser: closed {closed} idle job pages");
            }
            let (frozen, blanked) = rest_idle(&state, IDLE_FREEZE, IDLE_BLANK).await;
            if frozen + blanked > 0 {
                tracing::info!("browser: froze {frozen} and blanked {blanked} idle pages");
            }
            let disconnected = close_idle_connections(&state, CONN_IDLE_CLOSE).await;
            if disconnected > 0 {
                tracing::info!("browser: closed {disconnected} idle pages' connections");
            }
        }
    });
}

/// The page the team's live view shows: the page an open step holds (the person has something
/// to do there), else the page used most recently, else the team page.
pub async fn live_page(state: &AppState, team: &str) -> String {
    if let Ok(steps) = state.db.list_human_steps(Some("open"), None, None).await {
        if let Some(page) = steps.iter().filter(|s| s.sandbox_target.as_deref() == Some(team)).find_map(|s| s.page().map(str::to_string)) {
            return page;
        }
    }
    let prefix = format!("{team}{}", PAGE_SEP);
    state
        .browser_service
        .pages_in_use()
        .into_iter()
        .filter(|(k, _)| k == team || k.starts_with(&prefix))
        .max_by_key(|(_, at)| *at)
        .map(|(k, _)| k)
        .unwrap_or_else(|| team.to_string())
}

/// 29d: the address and title the tab list shows for a page: the newer of what the controller
/// holds (written by browser.rs, or by a view on the bot's page) and what a view on the page the
/// person picked last read there. The picked read is newer while the controller still holds
/// exactly what it held at that read ([`SeenLocation::over`]), or holds nothing yet.
pub(crate) fn shown_location(written: (Option<String>, Option<String>), seen: Option<SeenLocation>) -> (Option<String>, Option<String>) {
    match seen {
        Some(seen) if written.0.is_none() || seen.over == written => (Some(seen.url), Some(seen.title)),
        _ => written,
    }
}

/// `GET /api/sandbox/:team_id/pages`: the team's pages for a tab strip, newest first. Each has
/// its `page` key (pass it to the live view as `?page=`), the bot and chat of a job page, its
/// address and title, whether a step holds it (`held`), and seconds since its last use. `live`
/// is the page the live view shows by default.
pub async fn list_pages(State(state): State<AppState>, Path(team_id): Path<String>) -> Json<Value> {
    let team = page_team(&team_id).to_string();
    let now = Instant::now();
    let mut pages: Vec<(String, Option<Instant>)> = vec![(team.clone(), state.browser_service.page_used_at(&team))];
    pages.extend(state.browser_service.job_pages(&team).await.into_iter().map(|(k, at)| (k, Some(at))));
    pages.sort_by_key(|(_, at)| std::cmp::Reverse(*at));
    let mut out = Vec::new();
    for (key, at) in pages {
        let ctrl = state.browser_controllers.lock().await.get(&key).cloned();
        let written = match &ctrl {
            Some(c) => (c.current_url.lock().await.clone(), c.current_title.lock().await.clone()),
            None => (None, None),
        };
        let (url, title) = shown_location(written, state.browser_service.pages_seen().get(&key));
        let mut parts = key.splitn(3, PAGE_SEP).skip(1);
        out.push(json!({
            "page": key,
            "team_id": team,
            "kwant_id": parts.next(),
            "chat_id": parts.next(),
            "job": is_job_page(&key),
            "url": url,
            "title": title,
            "held": state.db.human_step_open_on(&key).await.unwrap_or(false),
            "idle_seconds": at.map(|at| now.duration_since(at).as_secs()),
        }));
    }
    Json(json!({
        "team_id": team,
        "live": live_page(&state, &team).await,
        "max_job_pages": MAX_JOB_PAGES_PER_TEAM,
        "pages": out,
    }))
}

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

    #[test]
    fn a_job_page_is_keyed_by_team_bot_and_chat() {
        assert_eq!(job_page_key("t", "k", "c").as_deref(), Some("t#k#c"));
        assert_eq!(job_page_key("t", "k", ""), None);
        assert_eq!(job_page_key("t", "k#x", "c"), None, "a part with the separator can't make a key");
        assert_eq!(page_team("t#k#c"), "t");
        assert_eq!(page_team("t"), "t");
        assert!(is_job_page("t#k#c") && !is_job_page("t"));
        assert_eq!(call_page("t", Some("k"), Some("c")), "t#k#c");
        assert_eq!(call_page("t", Some("k"), None), "t", "a bot route without a chat keeps the team page");
        assert_eq!(call_page("t", None, None), "t", "the person's panel keeps the team page");
    }

    #[test]
    fn a_job_pages_parts_are_read_back_from_its_key() {
        assert_eq!(job_of("t#k#c"), Some(("t", "k", "c")));
        assert_eq!(job_of("t"), None);
        assert!(pages_full_message().starts_with("All 4 of this team's browser pages are in use by other jobs."));
        let pages = JobPages::default();
        assert_eq!(pages.wait(), PAGE_WAIT);
        pages.set_wait(Duration::from_millis(5));
        assert_eq!(pages.wait(), Duration::from_millis(5));
        assert!(Arc::ptr_eq(&pages.team_lock("t"), &pages.team_lock("t")));
        assert!(!Arc::ptr_eq(&pages.team_lock("t"), &pages.team_lock("u")), "one lock per team");
    }

    #[test]
    fn retries_back_off_from_one_second_to_eight_inside_the_window() {
        let delays: Vec<u64> = (0..6).map(|n| retry_delay(n).as_secs()).collect();
        assert_eq!(delays, [1, 2, 4, 8, 8, 8]);
        assert_eq!(retry_delay(40), RETRY_MAX_DELAY, "no overflow on a long outage");
        let s = Duration::from_secs;
        assert!(keep_retrying(s(0), s(1), RETRY_WINDOW));
        assert!(keep_retrying(s(36), s(8), RETRY_WINDOW));
        assert!(!keep_retrying(s(37), s(8), RETRY_WINDOW), "the next try would start after the window");
        assert!(!keep_retrying(s(45), s(1), RETRY_WINDOW));
        // the waits alone fill the window in 1+2+4+8+8+8+8 = 39 s: about 7 tries when each fails fast
        let mut elapsed = Duration::ZERO;
        let mut tries = 1;
        while keep_retrying(elapsed, retry_delay(tries - 1), RETRY_WINDOW) {
            elapsed += retry_delay(tries - 1);
            tries += 1;
        }
        assert_eq!((tries, elapsed.as_secs()), (8, 39));
        let pages = JobPages::default();
        assert_eq!(pages.retry_window(), RETRY_WINDOW);
        pages.set_retry_window(Duration::from_millis(5));
        assert_eq!(pages.retry_window(), Duration::from_millis(5));
    }

    #[test]
    fn idle_pages_are_frozen_after_ten_minutes_and_team_pages_blanked_after_hours() {
        let m = |n: u64| Duration::from_secs(n * 60);
        let rest = |job, idle, rested| rest_for(job, idle, rested, IDLE_FREEZE, IDLE_BLANK);
        assert_eq!(rest(false, m(9), None), None, "used within ten minutes");
        assert_eq!(rest(false, m(10), None), Some(Rest::Frozen));
        assert_eq!(rest(true, m(10), None), Some(Rest::Frozen));
        assert_eq!(rest(false, m(30), Some(Rest::Frozen)), None, "already frozen");
        assert_eq!(rest(false, m(120), Some(Rest::Frozen)), Some(Rest::Blank));
        assert_eq!(rest(false, m(120), None), Some(Rest::Blank));
        assert_eq!(rest(false, m(600), Some(Rest::Blank)), None, "already blank");
        assert_eq!(rest(true, m(600), None), Some(Rest::Frozen), "a job page is never blanked (it is closed)");
    }

    #[test]
    fn a_pages_connections_never_close_mid_download_or_chooser_even_if_restable() {
        assert!(connection_closable(true, false, false));
        assert!(!connection_closable(false, false, false), "not restable (watched, held, or has a turn)");
        assert!(!connection_closable(true, true, false), "a download is in progress");
        assert!(!connection_closable(true, false, true), "a file chooser is open, unanswered");
        assert!(!connection_closable(true, true, true));
    }

    /// 31b: how many pages fit the budget for a number of teams, each team filling its pages in
    /// turn (at most four job pages each).
    fn pages_that_fit(teams: usize) -> usize {
        let mut open: Vec<String> = Vec::new();
        let mut jobs = vec![0; teams];
        'fill: loop {
            let mut added = false;
            for (t, n) in jobs.iter_mut().enumerate() {
                let team = format!("t{t}");
                let key = if open.contains(&team) {
                    if *n == MAX_JOB_PAGES_PER_TEAM {
                        continue;
                    }
                    *n += 1;
                    job_page_key(&team, "k", &format!("c{n}")).unwrap()
                } else {
                    team
                };
                if pids_with(&open, &key) > PAGE_PID_BUDGET {
                    break 'fill;
                }
                open.push(key);
                added = true;
            }
            if !added {
                break;
            }
        }
        open.len()
    }

    #[test]
    fn the_browsers_pages_stay_under_its_process_limit_with_room_for_normal_use() {
        assert_eq!(BROWSER_PIDS_LIMIT, 512);
        let args = crate::sandbox::service::managed_run_args("n", std::path::Path::new("/tmp"), None);
        assert!(args.contains(&format!("--pids-limit={BROWSER_PIDS_LIMIT}")), "the budget follows the container's real limit");
        assert_eq!(PAGE_PID_BUDGET, 328);
        // one or two teams never meet it; three busy teams keep 13 of their 15 pages
        assert_eq!(pages_that_fit(1), 1 + MAX_JOB_PAGES_PER_TEAM);
        assert_eq!(pages_that_fit(2), 2 * (1 + MAX_JOB_PAGES_PER_TEAM));
        assert_eq!(pages_that_fit(3), 13);
        assert!((4..=6).all(|teams| pages_that_fit(teams) >= 8), "never under the ~8 pages normal use needs");
        // a job page of a team with no page yet opens its team's page too; the page itself counts once
        assert_eq!(pids_with(&[], "t#k#c"), TEAM_PAGE_PIDS + JOB_PAGE_PIDS);
        assert_eq!(pids_with(&["t".into()], "t#k#c"), TEAM_PAGE_PIDS + JOB_PAGE_PIDS);
        assert_eq!(pids_with(&["t".into(), "t#k#c".into()], "t#k#c"), TEAM_PAGE_PIDS + JOB_PAGE_PIDS);
        assert_eq!(pids_with(&["u".into()], "t"), 2 * TEAM_PAGE_PIDS);
        assert!(browser_full_message().contains("nothing is wrong with the browser"));
        let pages = JobPages::default();
        assert_eq!(pages.page_budget(), PAGE_PID_BUDGET);
        pages.set_page_budget(60);
        assert_eq!(pages.page_budget(), 60);
    }

    #[test]
    fn a_turn_in_a_chat_uses_its_own_page_and_one_without_the_team_page() {
        let origin = super::super::continuity::TurnOrigin { thread_id: Some("th".into()), ..Default::default() };
        let scope = SelfScope { kwant_id: "k".into(), origin: Some(origin), sandbox_target: Some("t".into()), ..Default::default() };
        assert_eq!(scope_page(&scope).as_deref(), Some("t#k#th"));
        let no_chat = SelfScope { kwant_id: "k".into(), sandbox_target: Some("t".into()), ..Default::default() };
        assert_eq!(scope_page(&no_chat).as_deref(), Some("t"));
        let no_browser = SelfScope { kwant_id: "k".into(), ..Default::default() };
        assert_eq!(scope_page(&no_browser), None);
    }
}