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.

34.8 KB
Raw Blame
//! "Use kwant from your coding agent" (0.7.4): an MCP endpoint, `POST /api/mcp/agents`, through
//! which an outside coding agent (Claude Code, Codex, Cursor) asks this server's bots for work.
//!
//! The person makes a connection under `/api/agent-connections` (person-only) and gets a `kag_`
//! token once; only its sha256 is kept. The token works on [`ENDPOINT`] and nowhere else (the
//! auth guard answers 403 for it on every other route, from loopback too), and never counts as
//! the person. A connection sees only the bots in its spaces (and, when it names some, only those
//! bots), and only the threads and turns it started itself. Its messages are stored as the
//! person's chat messages with `meta.from_agent`, and every bot reads them framed as an outside
//! agent's (see [`crate::drivers::agent_message`]). Approvals don't change: a card goes to the
//! person, and the agent sees `waiting_on_person` with a one-line summary.
//!
//! Logs name a connection by id and name only; the token never reaches a log or a reply.

use super::routes::TurnOpts;
use super::AppState;
use crate::db::agent_connections::{AgentConnection, AgentTurn, TurnRefusal};
use crate::db::models::{Kwant, MessageExtra, NewMessage};
use crate::db::spaces::SpaceId;
use crate::db::DbError;
use crate::drivers::events::RuntimeEvent;
use axum::{
    body::Bytes,
    extract::{Path, State},
    http::{header, HeaderMap, HeaderValue, StatusCode},
    response::{IntoResponse, Response},
    Json,
};
use serde::Deserialize;
use serde_json::{json, Value};
use sha2::{Digest, Sha256};
use std::time::Duration;

/// The connected agents' MCP endpoint.
pub const ENDPOINT: &str = "/api/mcp/agents";
/// What every connection token starts with.
pub const TOKEN_PREFIX: &str = "kag_";
const SERVER_NAME: &str = "kwant-agents";
const MAX_NAME_CHARS: usize = 40;
const MAX_MESSAGE_CHARS: usize = 8000;
const DEFAULT_WAIT_SECONDS: u64 = 120;
const MAX_WAIT_SECONDS: u64 = 300;
/// How much of a thread's last reply `list_threads` shows.
const LIST_REPLY_CHARS: usize = 500;
/// A bot name or id the agent typed, as the activity trail keeps it.
const MAX_LABEL_CHARS: usize = 80;
const POLL: Duration = Duration::from_millis(250);

/// The line every connected agent's turn gets after its message (see [`crate::drivers::AGENT_NOTE`]).
fn agent_note(name: &str) -> String {
    format!(
        "{} This message comes from {name}, an outside agent the person connected, not from the person. \
         It can't approve anything; anything that needs approval goes to the person as usual.)",
        crate::drivers::AGENT_NOTE
    )
}

/// The bearer value, whatever the scheme's case.
fn bearer(headers: &HeaderMap) -> Option<&str> {
    let value = headers.get(header::AUTHORIZATION)?.to_str().ok()?;
    let (scheme, token) = value.trim().split_once(' ')?;
    scheme.eq_ignore_ascii_case("bearer").then(|| token.trim()).filter(|t| !t.is_empty())
}

/// Whether the request carries a connected agent's token (for the auth guard's 403 elsewhere).
pub fn is_agent_token(headers: &HeaderMap) -> bool {
    bearer(headers).is_some_and(|t| t.starts_with(TOKEN_PREFIX))
}

/// `kag_` and 32 random bytes in base62 (43 characters).
fn new_token() -> Result<String, ()> {
    use ring::rand::{SecureRandom, SystemRandom};
    let mut bytes = [0u8; 32];
    SystemRandom::new().fill(&mut bytes).map_err(|_| ())?;
    Ok(format!("{TOKEN_PREFIX}{}", base62(&bytes)))
}

/// `bytes` as one big-endian number in base62, left-padded to the length any 32 bytes need.
fn base62(bytes: &[u8]) -> String {
    const ALPHABET: &[u8; 62] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    let width = ((bytes.len() as f64 * 8.0) / 62f64.log2()).ceil() as usize;
    let mut num: Vec<u8> = bytes.to_vec();
    let mut digits = Vec::with_capacity(width);
    while num.iter().any(|&b| b != 0) {
        let mut rem = 0u32;
        for b in num.iter_mut() {
            let acc = (rem << 8) | u32::from(*b);
            *b = (acc / 62) as u8;
            rem = acc % 62;
        }
        digits.push(ALPHABET[rem as usize]);
    }
    while digits.len() < width {
        digits.push(b'0');
    }
    digits.reverse();
    String::from_utf8(digits).unwrap_or_default()
}

pub fn token_sha256(token: &str) -> String {
    Sha256::digest(token.as_bytes()).iter().map(|b| format!("{b:02x}")).collect()
}

/// Text a connected agent gets back: credentials, `kag_` tokens and home folders hidden.
pub fn redact(text: &str) -> String {
    let text = crate::profiles::classify::mask_credentials(text);
    let text = crate::drivers::activity::hide_home_paths(&text);
    let mut out = String::with_capacity(text.len());
    let mut rest = text.as_str();
    while let Some(at) = rest.find(TOKEN_PREFIX) {
        out.push_str(&rest[..at]);
        let after = &rest[at + TOKEN_PREFIX.len()..];
        let end = after.find(|c: char| !c.is_ascii_alphanumeric()).unwrap_or(after.len());
        if end >= 8 {
            out.push_str("kag_[redacted]");
        } else {
            out.push_str(&rest[at..at + TOKEN_PREFIX.len() + end]);
        }
        rest = &after[end..];
    }
    out.push_str(rest);
    out
}

fn cut(text: &str, max: usize) -> String {
    if text.chars().count() <= max {
        return text.to_string();
    }
    text.chars().take(max.saturating_sub(1)).collect::<String>() + "…"
}

fn bad_request(msg: &str) -> Response {
    (StatusCode::BAD_REQUEST, Json(json!({"error": msg}))).into_response()
}

fn internal() -> Response {
    (StatusCode::INTERNAL_SERVER_ERROR, Json(json!({"error": "something went wrong"}))).into_response()
}

// ---------------------------------------------------------------------------------------------
// The person's routes: /api/agent-connections (person-only in the auth guard)
// ---------------------------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
pub struct CreateConnection {
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub space_ids: Vec<String>,
    /// `"all"` or a list of bot ids.
    #[serde(default)]
    pub bots: Value,
}

/// A connection as the Settings card shows it: never the token or its hash.
fn connection_view(c: &AgentConnection) -> Value {
    json!({
        "id": c.id,
        "name": c.name,
        "space_ids": c.space_list(),
        "bots": match c.bot_list() {
            None => json!("all"),
            Some(ids) => json!(ids),
        },
        "created_at": c.created_at,
        "last_used_at": c.last_used_at,
    })
}

/// A connection's name, trimmed: 1 to 40 characters, no control characters or brackets (it is
/// quoted inside the frame a bot reads).
fn clean_name(raw: &str) -> Result<String, &'static str> {
    let name = raw.trim();
    let n = name.chars().count();
    if n == 0 || n > MAX_NAME_CHARS {
        return Err("name must be 1 to 40 characters");
    }
    if name.chars().any(|c| c.is_control() || matches!(c, '[' | ']' | '\u{FF3B}' | '\u{FF3D}' | '<' | '>')) {
        return Err("name can't contain line breaks, brackets or angle brackets");
    }
    Ok(name.to_string())
}

/// `POST /api/agent-connections`: `{connection, token}`, the token shown this once.
pub async fn create_connection(State(state): State<AppState>, Json(body): Json<CreateConnection>) -> Response {
    let name = match clean_name(&body.name) {
        Ok(n) => n,
        Err(e) => return bad_request(e),
    };
    let mut space_ids: Vec<String> = Vec::new();
    for id in body.space_ids.iter().map(|s| s.trim()) {
        if !id.is_empty() && !space_ids.iter().any(|s| s == id) {
            space_ids.push(id.to_string());
        }
    }
    if space_ids.is_empty() {
        return bad_request("space_ids needs at least one space");
    }
    let spaces = match state.db.list_spaces().await {
        Ok(s) => s,
        Err(_) => return internal(),
    };
    for id in &space_ids {
        match spaces.iter().find(|s| &s.id == id) {
            None => return bad_request("unknown space in space_ids"),
            Some(s) if s.archived_at.is_some() => return bad_request("an archived space can't be connected"),
            Some(_) => {}
        }
    }
    let bots: Option<Vec<String>> = match &body.bots {
        Value::String(s) if s == "all" => None,
        Value::Null => None,
        Value::Array(items) => {
            let mut ids = Vec::new();
            for item in items {
                let Some(id) = item.as_str().map(str::trim).filter(|s| !s.is_empty()) else {
                    return bad_request("bots must be \"all\" or a list of bot ids");
                };
                let Ok(Some(k)) = state.db.get_kwant(id).await else {
                    return bad_request("unknown bot in bots");
                };
                if !space_ids.contains(&k.space_id) {
                    return bad_request("every bot in bots must be in one of the connection's spaces");
                }
                if !ids.iter().any(|x| x == id) {
                    ids.push(id.to_string());
                }
            }
            if ids.is_empty() {
                return bad_request("bots needs at least one bot, or \"all\"");
            }
            Some(ids)
        }
        _ => return bad_request("bots must be \"all\" or a list of bot ids"),
    };
    let Ok(token) = new_token() else {
        return internal();
    };
    match state.db.create_agent_connection(&name, &token_sha256(&token), &space_ids, bots.as_deref()).await {
        Ok(c) => {
            tracing::info!(connection_id = %c.id, connection_name = %c.name, "agent connection created");
            (StatusCode::CREATED, Json(json!({"connection": connection_view(&c), "token": token}))).into_response()
        }
        Err(DbError::Conflict(msg)) => (StatusCode::CONFLICT, Json(json!({"error": msg}))).into_response(),
        Err(_) => internal(),
    }
}

/// `GET /api/agent-connections`: the live connections, newest first, without tokens.
pub async fn list_connections(State(state): State<AppState>) -> Response {
    match state.db.list_agent_connections().await {
        Ok(list) => Json(json!({
            "connections": list.iter().map(connection_view).collect::<Vec<_>>(),
            "max": crate::db::agent_connections::MAX_LIVE_CONNECTIONS,
        }))
        .into_response(),
        Err(_) => internal(),
    }
}

/// `DELETE /api/agent-connections/:id`: revoked at once; the next call with its token is a 401.
pub async fn revoke_connection(State(state): State<AppState>, Path(id): Path<String>) -> Response {
    match state.db.revoke_agent_connection(&id).await {
        Ok(true) => {
            tracing::info!(connection_id = %id, "agent connection revoked");
            Json(json!({"id": id, "revoked": true})).into_response()
        }
        Ok(false) => (StatusCode::NOT_FOUND, Json(json!({"error": "no such connection"}))).into_response(),
        Err(_) => internal(),
    }
}

/// `GET /api/agent-connections/:id/activity`: the last 100 calls, newest first.
pub async fn connection_activity(State(state): State<AppState>, Path(id): Path<String>) -> Response {
    match state.db.get_agent_connection(&id).await {
        Ok(Some(_)) => {}
        Ok(None) => return (StatusCode::NOT_FOUND, Json(json!({"error": "no such connection"}))).into_response(),
        Err(_) => return internal(),
    }
    match state.db.list_agent_calls(&id, 100).await {
        Ok(calls) => Json(json!({"calls": calls})).into_response(),
        Err(_) => internal(),
    }
}

// ---------------------------------------------------------------------------------------------
// The MCP endpoint
// ---------------------------------------------------------------------------------------------

fn rpc_result(id: Value, result: Value) -> Response {
    Json(json!({"jsonrpc": "2.0", "id": id, "result": result})).into_response()
}

fn rpc_error(id: Value, code: i64, message: &str) -> Response {
    Json(json!({"jsonrpc": "2.0", "id": id, "error": {"code": code, "message": message}})).into_response()
}

/// A tool's answer: the object as JSON text and as `structuredContent`.
fn tool_ok(id: Value, value: Value) -> Response {
    let text = serde_json::to_string_pretty(&value).unwrap_or_default();
    rpc_result(id, json!({"content": [{"type": "text", "text": text}], "structuredContent": value, "isError": false}))
}

fn tool_refused(id: Value, text: &str) -> Response {
    rpc_result(id, json!({"content": [{"type": "text", "text": text}], "isError": true}))
}

/// HTTP 429 with `Retry-After` and a JSON-RPC error carrying `data.retry_after`.
fn too_many(id: Value, message: &str, retry_after: i64) -> Response {
    let body = json!({
        "jsonrpc": "2.0",
        "id": id,
        "error": {"code": -32029, "message": message, "data": {"retry_after": retry_after}},
    });
    let mut resp = (StatusCode::TOO_MANY_REQUESTS, Json(body)).into_response();
    if let Ok(v) = HeaderValue::from_str(&retry_after.to_string()) {
        resp.headers_mut().insert(header::RETRY_AFTER, v);
    }
    resp
}

fn tools() -> Value {
    json!([
        {
            "name": "list_bots",
            "description": "The kwant bots this connection may ask: id, name, role, team, space, and whether each is busy or idle.",
            "inputSchema": {"type": "object", "properties": {}, "additionalProperties": false},
        },
        {
            "name": "ask",
            "description": "Ask a kwant bot to do something. The message goes into a thread named after this connection; \
                the bot answers there. Waits up to wait_seconds for the reply. Status is done, working (call get_reply \
                with turn_id later), waiting_on_person (the person has to approve or answer something first) or failed. \
                You can't approve anything: approvals always go to the person.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "bot": {"type": "string", "description": "The bot's id, or its exact name."},
                    "message": {"type": "string", "maxLength": MAX_MESSAGE_CHARS},
                    "wait_seconds": {"type": "integer", "minimum": 0, "maximum": MAX_WAIT_SECONDS, "default": DEFAULT_WAIT_SECONDS},
                    "thread": {"type": "string", "description": "\"standing\" (default: one thread per bot for this connection), \"new\", or a thread_id this connection started."},
                },
                "required": ["bot", "message"],
                "additionalProperties": false,
            },
        },
        {
            "name": "get_reply",
            "description": "The state of a turn an earlier ask started (same shape as ask's answer).",
            "inputSchema": {
                "type": "object",
                "properties": {"turn_id": {"type": "string"}},
                "required": ["turn_id"],
                "additionalProperties": false,
            },
        },
        {
            "name": "list_threads",
            "description": "The threads this connection started, newest first, with the bot's last reply to this connection's asks in each (first 500 characters; null before the first reply).",
            "inputSchema": {
                "type": "object",
                "properties": {"bot": {"type": "string", "description": "Only threads with this bot (id or exact name)."}},
                "additionalProperties": false,
            },
        },
    ])
}

/// `POST /api/mcp/agents`: JSON-RPC (initialize, ping, tools/list, tools/call), JSON responses.
/// Every call needs a live connection's bearer, from loopback too; anything else is a bare 401.
pub async fn endpoint(State(state): State<AppState>, headers: HeaderMap, body: Bytes) -> Response {
    let conn = match bearer(&headers).filter(|t| t.starts_with(TOKEN_PREFIX)) {
        Some(token) => state.db.agent_connection_by_hash(&token_sha256(token)).await.ok().flatten(),
        None => None,
    };
    let Some(conn) = conn else {
        return StatusCode::UNAUTHORIZED.into_response();
    };
    let Ok(msg) = serde_json::from_slice::<Value>(&body) else {
        return (StatusCode::BAD_REQUEST, Json(json!({"jsonrpc": "2.0", "id": null, "error": {"code": -32700, "message": "parse error"}})))
            .into_response();
    };
    let Some(id) = msg.get("id").cloned() else {
        return StatusCode::ACCEPTED.into_response(); // notification
    };
    match msg["method"].as_str().unwrap_or("") {
        "initialize" => rpc_result(
            id,
            json!({
                "protocolVersion": msg["params"]["protocolVersion"].as_str().unwrap_or(crate::mcp::client::PROTOCOL_VERSION),
                "capabilities": {"tools": {}},
                "serverInfo": {"name": SERVER_NAME, "version": env!("CARGO_PKG_VERSION")},
                "instructions": "Ask the person's kwant bots for work. Approvals always go to the person; you see waiting_on_person.",
            }),
        ),
        "ping" => rpc_result(id, json!({})),
        "tools/list" => rpc_result(id, json!({"tools": tools()})),
        "tools/call" => {
            let _ = state.db.touch_agent_connection(&conn.id).await;
            let name = msg["params"]["name"].as_str().unwrap_or("").to_string();
            let args = msg["params"]["arguments"].clone();
            let (resp, bot, ok) = call_tool(&state, &conn, &name, &args, id).await;
            let tool = if tools().as_array().is_some_and(|t| t.iter().any(|x| x["name"] == name.as_str())) {
                name.as_str()
            } else {
                "unknown"
            };
            if let Err(e) = state.db.log_agent_call(&conn, tool, bot.as_deref(), ok).await {
                tracing::warn!(connection_id = %conn.id, error = %e, "agent call not logged");
            }
            resp
        }
        _ => rpc_error(id, -32601, "method not found"),
    }
}

/// Runs one tool: (response, the bot it was about, ok).
async fn call_tool(
    state: &AppState,
    conn: &AgentConnection,
    name: &str,
    args: &Value,
    id: Value,
) -> (Response, Option<String>, bool) {
    match name {
        "list_bots" => match list_bots(state, conn).await {
            Ok(bots) => (tool_ok(id, json!({"bots": bots})), None, true),
            Err(_) => (tool_refused(id, "Couldn't list the bots."), None, false),
        },
        "ask" => ask(state, conn, args, id).await,
        "get_reply" => {
            let turn_id = args["turn_id"].as_str().unwrap_or("").trim();
            match state.db.get_agent_turn(turn_id).await {
                Ok(Some(t)) if t.connection_id == conn.id => {
                    let bot = state.db.get_kwant(&t.kwant_id).await.ok().flatten().map(|k| k.name);
                    (tool_ok(id, turn_view(state, &t).await), bot, true)
                }
                // Another connection's turn reads as one that doesn't exist
                _ => (tool_refused(id, "No such turn for this connection."), None, false),
            }
        }
        "list_threads" => {
            let bot = match args["bot"].as_str().map(str::trim).filter(|s| !s.is_empty()) {
                Some(b) => match resolve_bot(state, conn, b).await {
                    Ok(k) => Some(k),
                    Err(e) => return (tool_refused(id, &e), Some(cut(b, MAX_LABEL_CHARS)), false),
                },
                None => None,
            };
            match list_threads(state, conn, bot.as_ref()).await {
                Ok(threads) => (tool_ok(id, json!({"threads": threads})), bot.map(|k| k.name), true),
                Err(_) => (tool_refused(id, "Couldn't list the threads."), None, false),
            }
        }
        _ => (tool_refused(id, "Unknown tool."), None, false),
    }
}

/// Every bot this connection may ask, in its live spaces.
async fn bots_in_scope(state: &AppState, conn: &AgentConnection) -> Result<Vec<Kwant>, DbError> {
    let allowed = conn.bot_list();
    let mut out = Vec::new();
    for space_id in conn.space_list() {
        let space = SpaceId::new(&space_id);
        match state.db.get_space(&space).await? {
            Some(s) if s.archived_at.is_none() => {}
            _ => continue,
        }
        for k in state.db.list_kwants(&space).await? {
            if allowed.as_ref().is_none_or(|ids| ids.contains(&k.id)) {
                out.push(k);
            }
        }
    }
    Ok(out)
}

/// A bot in scope by id, or by exact name when only one bot in scope has it.
async fn resolve_bot(state: &AppState, conn: &AgentConnection, wanted: &str) -> Result<Kwant, String> {
    let bots = bots_in_scope(state, conn).await.map_err(|_| "Couldn't load the bots.".to_string())?;
    if let Some(k) = bots.iter().find(|k| k.id == wanted) {
        return Ok(k.clone());
    }
    let named: Vec<&Kwant> = bots.iter().filter(|k| k.name == wanted).collect();
    match named.as_slice() {
        [k] => Ok((*k).clone()),
        [] => Err("This connection can't ask that bot. Call list_bots for the ones it can.".into()),
        _ => Err("More than one bot has that name; use its id from list_bots.".into()),
    }
}

async fn list_bots(state: &AppState, conn: &AgentConnection) -> Result<Vec<Value>, DbError> {
    let bots = bots_in_scope(state, conn).await?;
    let busy: std::collections::HashSet<String> = state.turns.active_turns().into_iter().map(|t| t.kwant_id).collect();
    let spaces = state.db.list_spaces().await?;
    let mut teams_by_space = std::collections::HashMap::new();
    let mut out = Vec::new();
    for k in bots {
        if !teams_by_space.contains_key(&k.space_id) {
            let teams = state.db.list_teams(&SpaceId::new(&k.space_id)).await.unwrap_or_default();
            teams_by_space.insert(k.space_id.clone(), teams);
        }
        let team = teams_by_space.get(&k.space_id).and_then(|teams| {
            teams
                .iter()
                .find(|t| {
                    t.lead_kwant_id == k.id
                        || serde_json::from_str::<Vec<String>>(&t.member_kwants).is_ok_and(|m| m.contains(&k.id))
                })
                .map(|t| t.name.clone())
        });
        let space = spaces.iter().find(|s| s.id == k.space_id).map(|s| s.name.clone());
        out.push(json!({
            "id": k.id,
            "name": k.name,
            "role": k.title,
            "team": team,
            "space": space,
            "state": if busy.contains(&k.id) { "busy" } else { "idle" },
        }));
    }
    Ok(out)
}

async fn list_threads(state: &AppState, conn: &AgentConnection, bot: Option<&Kwant>) -> Result<Vec<Value>, DbError> {
    let rows = state.db.list_agent_threads(&conn.id, bot.map(|k| k.id.as_str()), 50).await?;
    let mut out = Vec::new();
    for r in rows {
        let name = state.db.get_kwant(&r.kwant_id).await?.map(|k| k.name);
        let last = state.db.last_agent_reply_in_thread(&conn.id, &r.thread_id).await?.map(|t| cut(&redact(&t), LIST_REPLY_CHARS));
        out.push(json!({
            "thread_id": r.thread_id,
            "bot": {"id": r.kwant_id, "name": name},
            "title": r.title,
            "standing": r.standing,
            "updated_at": r.updated_at,
            "last_reply": last,
        }));
    }
    Ok(out)
}

/// `ask`: posts the message into the connection's thread with the bot, starts the bot's turn and
/// waits up to `wait_seconds` for it.
async fn ask(state: &AppState, conn: &AgentConnection, args: &Value, id: Value) -> (Response, Option<String>, bool) {
    let wanted = args["bot"].as_str().map(str::trim).unwrap_or("");
    // What the agent typed goes into the activity trail: kept short
    let bot_label = (!wanted.is_empty()).then(|| cut(wanted, MAX_LABEL_CHARS));
    if wanted.is_empty() {
        return (tool_refused(id, "bot is required."), None, false);
    }
    let message = args["message"].as_str().unwrap_or("").trim().to_string();
    if message.is_empty() {
        return (tool_refused(id, "message is required."), bot_label, false);
    }
    if message.chars().count() > MAX_MESSAGE_CHARS {
        return (tool_refused(id, "message is longer than 8000 characters."), bot_label, false);
    }
    let wait = match &args["wait_seconds"] {
        Value::Null => DEFAULT_WAIT_SECONDS,
        v => match v.as_u64() {
            Some(n) if n <= MAX_WAIT_SECONDS => n,
            _ => return (tool_refused(id, "wait_seconds must be 0 to 300."), bot_label, false),
        },
    };
    let kwant = match resolve_bot(state, conn, wanted).await {
        Ok(k) => k,
        Err(e) => return (tool_refused(id, &e), bot_label, false),
    };
    let bot_label = Some(kwant.name.clone());
    if super::spaces::ensure_active(state, &SpaceId::new(&kwant.space_id)).await.is_err() {
        return (tool_refused(id, "That bot's space is archived."), bot_label, false);
    }

    // The thread: this connection's standing one with the bot, a new one, or one it started
    let thread_arg = args["thread"].as_str().map(str::trim).filter(|s| !s.is_empty()).unwrap_or("standing");
    let thread_id = match thread_arg {
        "standing" => match state.db.standing_agent_thread(&conn.id, &kwant.id).await {
            Ok(Some(t)) => t,
            Ok(None) => match open_thread(state, conn, &kwant, true).await {
                Ok(t) => t,
                Err(_) => return (tool_refused(id, "Couldn't open a thread."), bot_label, false),
            },
            Err(_) => return (tool_refused(id, "Couldn't open a thread."), bot_label, false),
        },
        "new" => match open_thread(state, conn, &kwant, false).await {
            Ok(t) => t,
            Err(_) => return (tool_refused(id, "Couldn't open a thread."), bot_label, false),
        },
        given => {
            let owner = state.db.agent_thread_owner(given).await.ok().flatten();
            let thread = state.db.get_thread(given).await.ok().flatten();
            match (owner, thread) {
                (Some(owner), Some(t)) if owner == conn.id && t.kwant_id == kwant.id && !t.archived => t.id,
                (Some(owner), Some(t)) if owner == conn.id && t.kwant_id != kwant.id => {
                    return (tool_refused(id, "That thread is with another bot."), bot_label, false)
                }
                (Some(owner), Some(_)) if owner == conn.id => {
                    return (tool_refused(id, "That thread is archived."), bot_label, false)
                }
                _ => return (tool_refused(id, "This connection can only use threads it started."), bot_label, false),
            }
        }
    };

    // Both limits, checked and reserved in one write
    let turn = match state.db.reserve_agent_turn(&conn.id, &kwant.id, &thread_id).await {
        Ok(Ok(t)) => t,
        Ok(Err(TurnRefusal::RateLimited { retry_after })) => {
            tracing::info!(connection_id = %conn.id, connection_name = %conn.name, "agent ask refused: hourly limit");
            return (too_many(id, "This connection has made 60 asks in the last hour.", retry_after), bot_label, false);
        }
        Ok(Err(TurnRefusal::InFlight)) => {
            tracing::info!(connection_id = %conn.id, connection_name = %conn.name, "agent ask refused: 3 turns in flight");
            return (too_many(id, "This connection already has 3 turns working. Wait for one to finish.", 30), bot_label, false);
        }
        Err(_) => return (tool_refused(id, "Couldn't start the turn."), bot_label, false),
    };

    match start_turn(state, conn, &kwant, &thread_id, &turn, message).await {
        Ok(()) => {}
        Err(e) => {
            let _ = state.db.finish_agent_turn(&turn.id, "failed", None, Some(&e)).await;
            return (tool_refused(id, &format!("Couldn't start the turn: {e}")), bot_label, false);
        }
    }

    // Wait for the reply, a card for the person, or the deadline
    let deadline = tokio::time::Instant::now() + Duration::from_secs(wait);
    loop {
        let current = state.db.get_agent_turn(&turn.id).await.ok().flatten();
        let view = match &current {
            Some(t) => turn_view(state, t).await,
            None => break,
        };
        if view["status"] != "working" || tokio::time::Instant::now() >= deadline {
            return (tool_ok(id, view), bot_label, true);
        }
        tokio::time::sleep(POLL.min(deadline.saturating_duration_since(tokio::time::Instant::now()))).await;
    }
    (tool_refused(id, "The turn went missing."), bot_label, false)
}

async fn open_thread(state: &AppState, conn: &AgentConnection, kwant: &Kwant, standing: bool) -> Result<String, DbError> {
    let title = format!("From {}", conn.name);
    let thread = state.db.create_thread(&kwant.id, &title, "chat", None, "user").await?;
    state.db.add_agent_thread(&thread.id, &conn.id, &kwant.id, standing).await?;
    let _ = state.event_sender.send(RuntimeEvent::ThreadUpdated { thread: serde_json::to_value(&thread).unwrap_or(Value::Null) });
    Ok(thread.id)
}

/// Saves the agent's message in the thread and starts the bot's turn in the background; the
/// turn's row ends "done" with the reply's id, or "failed".
async fn start_turn(
    state: &AppState,
    conn: &AgentConnection,
    kwant: &Kwant,
    thread_id: &str,
    turn: &AgentTurn,
    message: String,
) -> Result<(), String> {
    let user_msg = NewMessage {
        channel_id: None,
        direct_kwant_id: None,
        // A chat message as the person's would be, but marked: every bot reads it framed as an
        // outside agent's, and it never closes what a bot waits on the person for
        sender_role: "user".into(),
        sender_id: format!("agent:{}", conn.id),
        text: message.clone(),
        reasoning_content: None,
        tool_calls: None,
        tokens_used: message.len() as i64 / 4,
    };
    let extra = MessageExtra {
        thread_id: Some(thread_id.to_string()),
        meta: Some(json!({"from_agent": conn.name, "agent_connection_id": conn.id, "agent_turn_id": turn.id})),
    };
    let saved = state.db.insert_message_with(&user_msg, &extra).await.map_err(|_| "the message couldn't be saved".to_string())?;
    let _ = state.db.set_agent_turn_message(&turn.id, &saved.id).await;
    let _ = state.db.touch_thread(thread_id).await;
    let _ = state.event_sender.send(RuntimeEvent::MessageAdded { message: serde_json::to_value(&saved).unwrap_or(Value::Null) });

    // Its own chain: the chain budget and every per-turn check (the spend stop included) apply
    let job = state.turns.start_job([saved.id.clone(), thread_id.to_string()]);
    let scope = super::routes::scope_for(state, kwant, None, Some(thread_id.to_string()), &job, 0).await?;
    let mut opts = TurnOpts::default();
    opts.purpose = Some("connected_agent".into());
    // The reply is found by this, not as the thread's newest bot message: the person may be
    // talking to the same bot in this thread while the ask runs
    opts.extra_meta = Some(json!({"agent_turn_id": turn.id}));
    let prompt = format!("{message}\n\n{}", agent_note(&crate::drivers::frame_safe_name(&conn.name)));
    let target = kwant.clone();
    let db = state.db.clone();
    let (turn_id, thread_id) = (turn.id.clone(), thread_id.to_string());
    let (conn_id, conn_name) = (conn.id.clone(), conn.name.clone());
    tracing::info!(connection_id = %conn_id, connection_name = %conn_name, kwant_id = %target.id, agent_turn = %turn_id, "agent ask started");
    tokio::spawn(async move {
        let _job = job;
        let result = super::routes::try_kwant_turn(&scope, &target, prompt, opts).await;
        match result {
            Ok(_) => {
                let reply = db.agent_turn_reply_id(&thread_id, &turn_id).await.ok().flatten();
                let _ = db.finish_agent_turn(&turn_id, "done", reply.as_deref(), None).await;
            }
            Err(e) => {
                let reason = cut(&redact(&e), 300);
                tracing::info!(connection_id = %conn_id, connection_name = %conn_name, agent_turn = %turn_id, "agent ask failed");
                let _ = db.finish_agent_turn(&turn_id, "failed", None, Some(&reason)).await;
            }
        }
    });
    Ok(())
}

/// `{status, reply_text, attachments, thread_id, turn_id}` for a turn.
async fn turn_view(state: &AppState, t: &AgentTurn) -> Value {
    let attachments: Vec<Value> = state
        .db
        .agent_turn_attachments(&t.thread_id, t.created_at, t.ended_at)
        .await
        .unwrap_or_default()
        .into_iter()
        .map(|a| json!({"name": a.name, "url": format!("/api/attachments/{}/content", a.id)}))
        .collect();
    let (status, reply_text) = match t.status.as_str() {
        "done" => {
            let text = match &t.reply_message_id {
                Some(mid) => state.db.get_message(mid).await.ok().flatten().map(|m| m.text).unwrap_or_default(),
                None => String::new(),
            };
            ("done", redact(&text))
        }
        "failed" => ("failed", t.error.clone().unwrap_or_else(|| "The turn failed.".into())),
        // Only cards the ask's own bot turns raised: its job holds the agent's message id
        _ => match async {
            let turn_ids = match &t.message_id {
                Some(mid) => state.turns.job_keys_holding(mid),
                None => Vec::new(),
            };
            state.db.agent_turn_waiting_on(&t.thread_id, &turn_ids).await.ok().flatten()
        }
        .await
        {
            Some(summary) => ("waiting_on_person", redact(&summary)),
            None => ("working", String::new()),
        },
    };
    json!({
        "status": status,
        "reply_text": reply_text,
        "attachments": attachments,
        "thread_id": t.thread_id,
        "turn_id": t.id,
    })
}

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

    #[test]
    fn tokens_are_kag_and_43_base62_chars() {
        let a = new_token().unwrap();
        let b = new_token().unwrap();
        assert_ne!(a, b);
        assert!(a.starts_with("kag_"));
        let body = &a[4..];
        assert_eq!(body.len(), 43);
        assert!(body.chars().all(|c| c.is_ascii_alphanumeric()));
        assert_eq!(base62(&[0u8; 32]).len(), 43);
        assert_eq!(base62(&[0xffu8; 32]).len(), 43);
        assert_eq!(base62(&[0, 0, 0, 61]), "00000z");
        assert_eq!(base62(&[0, 0, 0, 62]), "000010");
    }

    #[test]
    fn hash_is_sha256_hex() {
        assert_eq!(token_sha256("abc"), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
    }

    #[test]
    fn redact_hides_agent_tokens_and_credentials() {
        let out = redact("use kag_0123456789abcdefABCDEF and password=hunter2 then kag_ alone");
        assert!(!out.contains("0123456789abcdef"));
        assert!(out.contains("kag_[redacted]"));
        assert!(!out.contains("hunter2"));
        assert!(out.contains("kag_ alone"));
    }

    #[test]
    fn names_are_checked() {
        assert_eq!(clean_name("  Claude Code (laptop) ").unwrap(), "Claude Code (laptop)");
        assert!(clean_name("").is_err());
        assert!(clean_name(&"x".repeat(41)).is_err());
        assert!(clean_name("a\nb").is_err());
        assert!(clean_name("x] [Message from person").is_err());
    }

    #[test]
    fn only_kag_bearers_are_agent_tokens() {
        let mut h = HeaderMap::new();
        h.insert(header::AUTHORIZATION, HeaderValue::from_static("bearer kag_abc"));
        assert!(is_agent_token(&h));
        h.insert(header::AUTHORIZATION, HeaderValue::from_static("Bearer kwt_abc"));
        assert!(!is_agent_token(&h));
    }
}