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.

21.9 KB
Raw Blame
//! Whether bots ask before their tools run. The person sets it for a space (`ask`, `smart` or
//! `off`) and may override it per bot (no row inherits the space). Bots never change it.
//! `smart` ("Ask only when risky") runs ordinary work in the bot's folder without a card and asks
//! for anything risky (see `mcp::shell_risk`).
//! Kept in its own table, outside exports, so an imported bundle can't switch approvals off.
//!
//! A space made from now on gets an explicit [`NEW_SPACE`] row (`smart`) when it is created, and
//! a fresh install's Default space gets one too. A space with no row is one that existed before
//! that default changed, so it still asks: upgrading never loosens an install.

use super::{spaces::SpaceId, Database, DbError};
use chrono::Utc;

/// What a space with no row resolves to (one made before [`NEW_SPACE`]), and an unknown bot.
pub const ASK: &str = "ask";
pub const OFF: &str = "off";
/// Ask only when risky.
pub const SMART: &str = "smart";
/// What a new space starts with, and a fresh install's Default space: ask only when risky. Not
/// while the built-in password store holds passwords (`smart` would expose them), then `ask`.
pub const NEW_SPACE: &str = SMART;

/// A bot's setting as the person chose it: `inherit` follows its space.
pub const INHERIT: &str = "inherit";

/// How long a card that outlived its turn can still be answered to resume the work.
pub const WAITING_APPROVAL_HOURS: i64 = 24;

/// The status of an approvals record for a customer-turn call nobody allowed (16d): closed from
/// the start, nothing to answer.
pub const CUSTOMER_REFUSED: &str = "refused";

/// What `approvals` resolves to for one bot.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct ApprovalMode {
    /// The space's value: `ask`, `smart` or `off`.
    pub space: String,
    /// The bot's own choice: `inherit`, `ask`, `smart` or `off`.
    pub bot: String,
    /// The bot's override, else the space's: `ask`, `smart` or `off`.
    pub effective: String,
}

impl ApprovalMode {
    pub fn is_off(&self) -> bool {
        self.effective == OFF
    }

    pub fn is_smart(&self) -> bool {
        self.effective == SMART
    }
}

/// Whether the built-in password store has any file in `data_dir` (a database deleted next to a
/// vault it left behind). Read errors count as yes, so the Default space then asks.
fn holds_local_passwords(data_dir: &std::path::Path) -> bool {
    match std::fs::read_dir(data_dir.join(crate::sandbox::credentials::VAULT_DIR)) {
        Ok(mut entries) => entries.next().is_some(),
        Err(e) => e.kind() != std::io::ErrorKind::NotFound,
    }
}

/// `ask`, `smart` or `off`; anything else is not a mode.
pub fn valid_mode(mode: &str) -> bool {
    matches!(mode, ASK | SMART | OFF)
}

const CREATE_APPROVAL_MODES: &str = "CREATE TABLE IF NOT EXISTS approval_modes (
    scope TEXT NOT NULL CHECK (scope IN ('space', 'kwant')),
    id TEXT NOT NULL,
    mode TEXT NOT NULL CHECK (mode IN ('ask', 'smart', 'off')),
    updated_at TIMESTAMP NOT NULL,
    PRIMARY KEY (scope, id)
)";

impl Database {
    /// `fresh`: the database was just made, so its Default space is new and starts with
    /// [`NEW_SPACE`], unless `data_dir` already holds built-in saved passwords.
    pub(super) async fn migrate_approval_modes(&self, fresh: bool, data_dir: &std::path::Path) -> Result<(), DbError> {
        sqlx::query(CREATE_APPROVAL_MODES).execute(&self.pool).await?;
        // Tables made before `smart` existed only allow 'ask' and 'off': rebuild them once
        let sql: Option<(String,)> = sqlx::query_as("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'approval_modes'")
            .fetch_optional(&self.pool)
            .await?;
        if sql.is_some_and(|(sql,)| !sql.contains("'smart'")) {
            let mut tx = self.pool.begin().await?;
            sqlx::query("ALTER TABLE approval_modes RENAME TO approval_modes_old").execute(&mut *tx).await?;
            sqlx::query(CREATE_APPROVAL_MODES).execute(&mut *tx).await?;
            sqlx::query("INSERT INTO approval_modes (scope, id, mode, updated_at) SELECT scope, id, mode, updated_at FROM approval_modes_old")
                .execute(&mut *tx)
                .await?;
            sqlx::query("DROP TABLE approval_modes_old").execute(&mut *tx).await?;
            tx.commit().await?;
        }
        if fresh {
            let mode = if holds_local_passwords(data_dir) { ASK } else { NEW_SPACE };
            sqlx::query("INSERT OR IGNORE INTO approval_modes (scope, id, mode, updated_at) VALUES ('space', ?, ?, ?)")
                .bind(super::spaces::DEFAULT_SPACE_ID)
                .bind(mode)
                .bind(Utc::now())
                .execute(&self.pool)
                .await?;
        }
        Ok(())
    }

    async fn approval_mode_row(&self, scope: &str, id: &str) -> Result<Option<String>, DbError> {
        let row: Option<(String,)> = sqlx::query_as("SELECT mode FROM approval_modes WHERE scope = ? AND id = ?")
            .bind(scope)
            .bind(id)
            .fetch_optional(&self.pool)
            .await?;
        Ok(row.map(|r| r.0))
    }

    /// The space's approvals: `ask`, `smart` or `off`. No row (a space older than [`NEW_SPACE`])
    /// is `ask`.
    pub async fn space_approvals(&self, space: &SpaceId) -> Result<String, DbError> {
        Ok(self.approval_mode_row("space", space.as_str()).await?.unwrap_or_else(|| ASK.into()))
    }

    pub async fn set_space_approvals(&self, space: &SpaceId, mode: &str) -> Result<(), DbError> {
        sqlx::query(
            "INSERT INTO approval_modes (scope, id, mode, updated_at) VALUES ('space', ?, ?, ?)
             ON CONFLICT(scope, id) DO UPDATE SET mode = excluded.mode, updated_at = excluded.updated_at",
        )
        .bind(space.as_str())
        .bind(mode)
        .bind(Utc::now())
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    /// Sets a bot's override; `inherit` removes it.
    pub async fn set_kwant_approvals(&self, kwant_id: &str, mode: &str) -> Result<(), DbError> {
        if mode == INHERIT {
            sqlx::query("DELETE FROM approval_modes WHERE scope = 'kwant' AND id = ?").bind(kwant_id).execute(&self.pool).await?;
            return Ok(());
        }
        sqlx::query(
            "INSERT INTO approval_modes (scope, id, mode, updated_at) VALUES ('kwant', ?, ?, ?)
             ON CONFLICT(scope, id) DO UPDATE SET mode = excluded.mode, updated_at = excluded.updated_at",
        )
        .bind(kwant_id)
        .bind(mode)
        .bind(Utc::now())
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    /// Whether approvals are off, or only ask when risky, for any existing space or bot. Both let
    /// a bot run code without a card, so both keep passwords out of the built-in store. A space
    /// counts even when every bot in it overrides to `ask`: a new bot there would inherit it.
    /// An archived space and the bots in it don't count (0.7.3 A1): nothing runs there, and its
    /// approvals can't be changed until it is restored, which would leave the gate shut for good.
    pub async fn any_approvals_off(&self) -> Result<bool, DbError> {
        let row: Option<(i64,)> = sqlx::query_as(
            "SELECT 1 FROM approval_modes WHERE mode IN ('off', 'smart') AND (
                (scope = 'space' AND id IN (SELECT id FROM spaces WHERE archived_at IS NULL))
                OR (scope = 'kwant' AND id IN (
                    SELECT k.id FROM kwants k JOIN spaces s ON s.id = k.space_id WHERE s.archived_at IS NULL
                ))
            ) LIMIT 1",
        )
        .fetch_optional(&self.pool)
        .await?;
        Ok(row.is_some())
    }

    /// Whether `space` itself, or a bot in it, is on approvals off or "Ask only when risky",
    /// archived or not: what restoring it would bring back into [`Self::any_approvals_off`].
    pub async fn space_approvals_off(&self, space: &SpaceId) -> Result<bool, DbError> {
        let row: Option<(i64,)> = sqlx::query_as(
            "SELECT 1 FROM approval_modes WHERE mode IN ('off', 'smart') AND (
                (scope = 'space' AND id = ?1)
                OR (scope = 'kwant' AND id IN (SELECT id FROM kwants WHERE space_id = ?1))
            ) LIMIT 1",
        )
        .bind(space.as_str())
        .fetch_optional(&self.pool)
        .await?;
        Ok(row.is_some())
    }

    /// A bot's approvals in `space`: its override, else the space's.
    pub async fn kwant_approvals_in(&self, space: &SpaceId, kwant_id: &str) -> Result<ApprovalMode, DbError> {
        let space_mode = self.space_approvals(space).await?;
        let bot = self.approval_mode_row("kwant", kwant_id).await?;
        let effective = bot.clone().unwrap_or_else(|| space_mode.clone());
        Ok(ApprovalMode { space: space_mode, bot: bot.unwrap_or_else(|| INHERIT.into()), effective })
    }

    /// A bot's approvals, in the space it lives in. An unknown bot asks.
    pub async fn kwant_approvals(&self, kwant_id: &str) -> Result<ApprovalMode, DbError> {
        match self.space_of_kwant(kwant_id).await? {
            Some(space) => self.kwant_approvals_in(&space, kwant_id).await,
            None => Ok(ApprovalMode { space: ASK.into(), bot: INHERIT.into(), effective: ASK.into() }),
        }
    }

    /// The approvals rows of a space's bots, newest first (max 200), optionally of one status
    /// (`auto_allowed` lists what ran while approvals were off) or one bot.
    pub async fn list_approvals(
        &self,
        space: &SpaceId,
        status: Option<&str>,
        kwant_id: Option<&str>,
    ) -> Result<Vec<super::models::Approval>, DbError> {
        Ok(sqlx::query_as::<_, super::models::Approval>(
            "SELECT a.* FROM approvals a JOIN kwants k ON k.id = a.kwant_id
             WHERE k.space_id = ?1 AND (?2 IS NULL OR a.status = ?2) AND (?3 IS NULL OR a.kwant_id = ?3)
             ORDER BY a.created_at DESC, a.rowid DESC LIMIT 200",
        )
        .bind(space.as_str())
        .bind(status)
        .bind(kwant_id)
        .fetch_all(&self.pool)
        .await?)
    }

    /// A card nobody answered before its turn's wait ran out: it stays open as `waiting`, with
    /// the chat (`{"channel_id"|"thread_id"|"direct_kwant_id": …}`) a late Allow resumes in.
    /// False when the ticket was no longer pending.
    pub async fn park_approval(&self, id: &str, chat: &serde_json::Value) -> Result<bool, DbError> {
        let done = sqlx::query("UPDATE approvals SET status = 'waiting', resume = ? WHERE id = ? AND status = 'pending'")
            .bind(chat.to_string())
            .bind(id)
            .execute(&self.pool)
            .await?;
        Ok(done.rows_affected() == 1)
    }

    /// Records the chat a pending card is in (`{"channel_id"|"thread_id"|"direct_kwant_id": …}`),
    /// the same one [`Self::park_approval`] keeps.
    pub async fn set_approval_chat(&self, id: &str, chat: &serde_json::Value) -> Result<(), DbError> {
        sqlx::query("UPDATE approvals SET resume = ? WHERE id = ? AND status = 'pending'")
            .bind(chat.to_string())
            .bind(id)
            .execute(&self.pool)
            .await?;
        Ok(())
    }

    /// The chat approval `id`'s card is in, as [`Self::set_approval_chat`] stored it; `None` when
    /// the ticket is unknown or was stored without one.
    pub async fn approval_chat(&self, id: &str) -> Result<Option<serde_json::Value>, DbError> {
        let row: Option<(Option<String>,)> = sqlx::query_as("SELECT resume FROM approvals WHERE id = ?")
            .bind(id)
            .fetch_optional(&self.pool)
            .await?;
        Ok(row.and_then(|(r,)| r).and_then(|r| serde_json::from_str(&r).ok()))
    }

    /// Cards left `pending` by an earlier run of kwant: nothing waits on them any more, so they
    /// become `waiting` (answerable, a late Allow resumes the work). A card stored before its chat
    /// was recorded gets the chat of the turn that asked, else the bot's direct chat. Returns how
    /// many. Only for startup, while no turn is running.
    pub async fn park_orphaned_approvals(&self) -> Result<u64, DbError> {
        let done = sqlx::query(
            "UPDATE approvals SET status = 'waiting', resume = COALESCE(resume, \
                (SELECT CASE WHEN r.thread_id IS NOT NULL THEN json_object('thread_id', r.thread_id) \
                             WHEN r.channel_id IS NOT NULL THEN json_object('channel_id', r.channel_id) \
                             ELSE json_object('direct_kwant_id', COALESCE(r.direct_kwant_id, approvals.kwant_id)) END \
                 FROM running_turns r WHERE r.turn_id = approvals.turn_id), \
                json_object('direct_kwant_id', kwant_id)) \
             WHERE status = 'pending'",
        )
        .execute(&self.pool)
        .await?;
        Ok(done.rows_affected())
    }

    /// Whether a card of that turn is still `waiting` for an answer (its Allow resumes the work).
    pub async fn turn_has_waiting_approval(&self, turn_id: &str) -> Result<bool, DbError> {
        let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM approvals WHERE turn_id = ?1 AND status = 'waiting'")
            .bind(turn_id)
            .fetch_one(&self.pool)
            .await?;
        Ok(n > 0)
    }

    /// Answers a `waiting` card once: sets `status` and returns the ticket with its chat. `None`
    /// when it isn't waiting (answered, or never parked) or is older than [`WAITING_APPROVAL_HOURS`]
    /// (it stays `waiting` until [`Self::expire_waiting_approvals`] marks it `expired` and the
    /// person is told). Two answers at once: only one gets the ticket.
    pub async fn claim_waiting_approval(
        &self,
        id: &str,
        status: &str,
    ) -> Result<Option<(super::models::Approval, serde_json::Value)>, DbError> {
        let window = format!("-{WAITING_APPROVAL_HOURS} hours");
        let done = sqlx::query("UPDATE approvals SET status = ? WHERE id = ? AND status = 'waiting' AND created_at > datetime('now', ?)")
            .bind(status)
            .bind(id)
            .bind(&window)
            .execute(&self.pool)
            .await?;
        if done.rows_affected() != 1 {
            return Ok(None);
        }
        let chat = self.resume_of(id).await?;
        Ok(self.get_approval(id).await?.map(|a| (a, chat)))
    }

    async fn resume_of(&self, id: &str) -> Result<serde_json::Value, DbError> {
        let resume: Option<(Option<String>,)> =
            sqlx::query_as("SELECT resume FROM approvals WHERE id = ?").bind(id).fetch_optional(&self.pool).await?;
        Ok(resume.and_then(|r| r.0).and_then(|r| serde_json::from_str(&r).ok()).unwrap_or(serde_json::Value::Null))
    }

    /// Cards that waited [`WAITING_APPROVAL_HOURS`] without an answer become `expired`, each once
    /// (whoever else sweeps), returned with the chat they were in so the person can be told.
    pub async fn expire_waiting_approvals(&self) -> Result<Vec<(super::models::Approval, serde_json::Value)>, DbError> {
        let window = format!("-{WAITING_APPROVAL_HOURS} hours");
        let ids: Vec<(String,)> = sqlx::query_as("SELECT id FROM approvals WHERE status = 'waiting' AND created_at <= datetime('now', ?) ORDER BY created_at")
            .bind(&window)
            .fetch_all(&self.pool)
            .await?;
        let mut out = Vec::new();
        for (id,) in ids {
            let done = sqlx::query("UPDATE approvals SET status = 'expired' WHERE id = ? AND status = 'waiting'").bind(&id).execute(&self.pool).await?;
            if done.rows_affected() == 1 {
                if let Some(a) = self.get_approval(&id).await? {
                    out.push((a, self.resume_of(&id).await?));
                }
            }
        }
        Ok(out)
    }

    /// The person pressed Retry on an expired card: `expired` → `retried`, once. Returns the
    /// ticket with its chat; `None` when it isn't expired (already retried, or never expired).
    pub async fn claim_expired_approval(&self, id: &str) -> Result<Option<(super::models::Approval, serde_json::Value)>, DbError> {
        let done = sqlx::query("UPDATE approvals SET status = 'retried' WHERE id = ? AND status = 'expired'").bind(id).execute(&self.pool).await?;
        if done.rows_affected() != 1 {
            return Ok(None);
        }
        let chat = self.resume_of(id).await?;
        Ok(self.get_approval(id).await?.map(|a| (a, chat)))
    }

    /// Cards still `waiting` for a late answer (younger than [`WAITING_APPROVAL_HOURS`]), with
    /// the chat each one resumes in, newest first.
    pub async fn away_waiting_approvals(&self) -> Result<Vec<super::away::AwayRow>, DbError> {
        let window = format!("-{WAITING_APPROVAL_HOURS} hours");
        Ok(sqlx::query_as(
            "SELECT 'approval' AS kind, a.id, a.kwant_id, k.name AS kwant_name, a.tool_name AS title, a.status,
                    a.parameters AS body, a.created_at AS at,
                    json_extract(a.resume, '$.thread_id') AS thread_id,
                    json_extract(a.resume, '$.channel_id') AS channel_id,
                    json_extract(a.resume, '$.direct_kwant_id') AS direct_kwant_id, NULL AS choices
             FROM approvals a LEFT JOIN kwants k ON k.id = a.kwant_id
             WHERE a.status = 'waiting' AND a.created_at > datetime('now', ?)
             ORDER BY a.created_at DESC, a.rowid DESC",
        )
        .bind(window)
        .fetch_all(&self.pool)
        .await?)
    }

    /// Moves a card's `created_at` back `hours` (tests of the waiting window).
    #[doc(hidden)]
    pub async fn backdate_approval_for_test(&self, id: &str, hours: i64) -> Result<(), DbError> {
        sqlx::query("UPDATE approvals SET created_at = datetime('now', ?) WHERE id = ?")
            .bind(format!("-{hours} hours"))
            .bind(id)
            .execute(&self.pool)
            .await?;
        Ok(())
    }

    /// Records a call kwnt refused on its own (it named kwnt's secret files), as `denied`.
    pub async fn save_refused(&self, input: &super::models::NewApproval) -> Result<String, DbError> {
        let id = uuid::Uuid::new_v4().to_string();
        sqlx::query("INSERT INTO approvals (id, turn_id, kwant_id, tool_name, parameters, status) VALUES (?, ?, ?, ?, ?, 'denied')")
            .bind(&id)
            .bind(&input.turn_id)
            .bind(&input.kwant_id)
            .bind(&input.tool_name)
            .bind(serde_json::to_string(&input.parameters)?)
            .execute(&self.pool)
            .await?;
        Ok(id)
    }

    /// Records a call in a customer's turn that kwnt's tool endpoint refused because nobody allowed
    /// it (16d), as [`CUSTOMER_REFUSED`]: already closed, so it is never a card, never answered and
    /// never resumed. It only shows in the bot's approvals list. Its id.
    ///
    /// 19e: one row per (turn, bot, tool, arguments). A model that retries the same refused call in
    /// the same turn gets the first row's id back and adds nothing; other arguments, or the same
    /// call in a later turn, add a row. Same arguments means the same stored details string. The
    /// check and the insert are one statement, so two refusals at once can't both add a row.
    ///
    /// 22a: `chat` is the chat the call happened in, stored in `resume` in a card's shape
    /// (`{"thread_id"|"channel_id"|"direct_kwant_id": …}`). It only names the place: the row stays
    /// closed, and the chat is not part of "the same call" (a retry keeps the first row as it is).
    pub async fn save_customer_refused(
        &self,
        input: &super::models::NewApproval,
        chat: Option<&serde_json::Value>,
    ) -> Result<String, DbError> {
        let id = uuid::Uuid::new_v4().to_string();
        let parameters = serde_json::to_string(&input.parameters)?;
        let added = sqlx::query(
            "INSERT INTO approvals (id, turn_id, kwant_id, tool_name, parameters, status, resume) \
             SELECT ?1, ?2, ?3, ?4, ?5, ?6, ?7 WHERE NOT EXISTS (SELECT 1 FROM approvals \
             WHERE turn_id = ?2 AND kwant_id = ?3 AND tool_name = ?4 AND parameters = ?5 AND status = ?6)",
        )
        .bind(&id)
        .bind(&input.turn_id)
        .bind(&input.kwant_id)
        .bind(&input.tool_name)
        .bind(&parameters)
        .bind(CUSTOMER_REFUSED)
        .bind(chat.map(|c| c.to_string()))
        .execute(&self.pool)
        .await?
        .rows_affected();
        if added > 0 {
            return Ok(id);
        }
        let first: String = sqlx::query_scalar(
            "SELECT id FROM approvals WHERE turn_id = ? AND kwant_id = ? AND tool_name = ? AND parameters = ? AND status = ? \
             ORDER BY created_at, rowid LIMIT 1",
        )
        .bind(&input.turn_id)
        .bind(&input.kwant_id)
        .bind(&input.tool_name)
        .bind(&parameters)
        .bind(CUSTOMER_REFUSED)
        .fetch_one(&self.pool)
        .await?;
        Ok(first)
    }

    /// Records a call that ran without a card because approvals were off (or it was safe under
    /// "Ask only when risky").
    /// Records a call the person approved before it was asked (0.9.7: a push their Retry or
    /// Override on a pre-push card covers) as `allowed_once`, with no card. Its id.
    pub async fn save_granted(&self, input: &super::models::NewApproval) -> Result<String, DbError> {
        let id = uuid::Uuid::new_v4().to_string();
        sqlx::query("INSERT INTO approvals (id, turn_id, kwant_id, tool_name, parameters, status) VALUES (?, ?, ?, ?, ?, 'allowed_once')")
            .bind(&id)
            .bind(&input.turn_id)
            .bind(&input.kwant_id)
            .bind(&input.tool_name)
            .bind(serde_json::to_string(&input.parameters)?)
            .execute(&self.pool)
            .await?;
        Ok(id)
    }

    pub async fn save_auto_allowed(&self, input: &super::models::NewApproval) -> Result<String, DbError> {
        let id = uuid::Uuid::new_v4().to_string();
        sqlx::query(
            "INSERT INTO approvals (id, turn_id, kwant_id, tool_name, parameters, status) VALUES (?, ?, ?, ?, ?, 'auto_allowed')",
        )
        .bind(&id)
        .bind(&input.turn_id)
        .bind(&input.kwant_id)
        .bind(&input.tool_name)
        .bind(serde_json::to_string(&input.parameters)?)
        .execute(&self.pool)
        .await?;
        Ok(id)
    }
}