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.

24.6 KB
Raw Blame
//! What a bot is waiting on the person for: an open question (its card) or a reply that ended by
//! asking the person something. Each gets at most one reminder, `due_at` after the turn ended
//! (none when reminders are off). The person writing in that chat, or the question closing, ends it.

use super::{Database, DbError};
use chrono::{DateTime, Utc};
use serde::Serialize;
use sqlx::FromRow;
use uuid::Uuid;

/// "open" (still waiting), "reminded" (its one reminder went out, still waiting) or "closed".
#[derive(Debug, Clone, Serialize, FromRow, PartialEq)]
pub struct WaitingItem {
    pub id: String,
    pub kwant_id: String,
    pub channel_id: Option<String>,
    pub direct_kwant_id: Option<String>,
    pub thread_id: Option<String>,
    /// Set when the bot asked with a question card.
    pub question_id: Option<String>,
    /// The reply or card that asked.
    pub message_id: Option<String>,
    pub question: String,
    /// When the reminder goes out; `None` when reminders were off as the turn ended.
    pub due_at: Option<DateTime<Utc>>,
    pub status: String,
    pub created_at: DateTime<Utc>,
    pub reminded_at: Option<DateTime<Utc>>,
    /// `None`: a question or a reply that asked. `"stopped"`: the bot's work stopped without
    /// finishing and `question` says what stopped, why and what resumes it (0.9.5, see
    /// `server::stopped`). Never reminded (`due_at` is `None`).
    #[sqlx(default)]
    pub kind: Option<String>,
    /// Stopped items only: what Retry does, as JSON (`{"type":"approval","approval_id":…}`);
    /// `None` when the chat's own Retry (resending the person's message) resumes it.
    #[sqlx(default)]
    pub retry: Option<String>,
}

/// The `kind` of an item that says a bot's work stopped.
pub const STOPPED: &str = "stopped";

pub struct NewWaitingItem {
    pub kwant_id: String,
    pub channel_id: Option<String>,
    pub direct_kwant_id: Option<String>,
    pub thread_id: Option<String>,
    pub question_id: Option<String>,
    pub message_id: Option<String>,
    pub question: String,
    pub due_at: Option<DateTime<Utc>>,
}

/// The same bot in the same chat, matched with NULLs as "".
const SAME_CHAT: &str = "kwant_id = ? AND COALESCE(channel_id, '') = ? AND COALESCE(direct_kwant_id, '') = ? \
     AND COALESCE(thread_id, '') = ?";

impl Database {
    pub(super) async fn migrate_waiting(&self) -> Result<(), DbError> {
        for stmt in [
            "CREATE TABLE IF NOT EXISTS waiting_on_person (
                id TEXT PRIMARY KEY,
                kwant_id TEXT NOT NULL,
                channel_id TEXT,
                direct_kwant_id TEXT,
                thread_id TEXT,
                question_id TEXT,
                message_id TEXT,
                question TEXT NOT NULL,
                due_at TIMESTAMP,
                status TEXT NOT NULL CHECK (status IN ('open', 'reminded', 'closed')),
                created_at TIMESTAMP NOT NULL,
                reminded_at TIMESTAMP
            )",
            "CREATE INDEX IF NOT EXISTS waiting_on_person_due ON waiting_on_person (status, due_at)",
            // One entry per question, ever: a question is never reminded twice
            "CREATE UNIQUE INDEX IF NOT EXISTS waiting_on_person_question ON waiting_on_person (question_id) WHERE question_id IS NOT NULL",
        ] {
            sqlx::query(stmt).execute(&self.pool).await?;
        }
        // 0.9.5: stopped items (additive; an older kwnt reads them as plain replies)
        for (col, ty) in [("kind", "TEXT"), ("retry", "TEXT")] {
            let has: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM pragma_table_info('waiting_on_person') WHERE name = ?")
                .bind(col)
                .fetch_one(&self.pool)
                .await?;
            if has == 0 {
                sqlx::query(&format!("ALTER TABLE waiting_on_person ADD COLUMN {col} {ty}")).execute(&self.pool).await?;
            }
        }
        Ok(())
    }

    /// Records what a bot waits for. `None` when it is already recorded: the same question, or
    /// for a plain ask, an earlier one of this bot in this chat still waiting (it keeps its time,
    /// so asking again doesn't bring a second reminder).
    pub async fn add_waiting(&self, w: &NewWaitingItem) -> Result<Option<WaitingItem>, DbError> {
        let mut tx = self.begin_write().await?;
        let existing: Option<(String,)> = match &w.question_id {
            Some(q) => sqlx::query_as("SELECT id FROM waiting_on_person WHERE question_id = ?").bind(q).fetch_optional(&mut *tx).await?,
            None => {
                sqlx::query_as(&format!(
                    "SELECT id FROM waiting_on_person WHERE question_id IS NULL AND kind IS NULL AND status != 'closed' AND {SAME_CHAT}"
                ))
                .bind(&w.kwant_id)
                .bind(w.channel_id.as_deref().unwrap_or(""))
                .bind(w.direct_kwant_id.as_deref().unwrap_or(""))
                .bind(w.thread_id.as_deref().unwrap_or(""))
                .fetch_optional(&mut *tx)
                .await?
            }
        };
        if existing.is_some() {
            tx.rollback().await?;
            return Ok(None);
        }
        let row = sqlx::query_as::<_, WaitingItem>(
            "INSERT INTO waiting_on_person (id, kwant_id, channel_id, direct_kwant_id, thread_id, question_id, message_id, question, due_at, status, created_at) \
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'open', ?) RETURNING *",
        )
        .bind(Uuid::new_v4().to_string())
        .bind(&w.kwant_id)
        .bind(&w.channel_id)
        .bind(&w.direct_kwant_id)
        .bind(&w.thread_id)
        .bind(&w.question_id)
        .bind(&w.message_id)
        .bind(&w.question)
        .bind(w.due_at)
        .bind(Utc::now())
        .fetch_one(&mut *tx)
        .await?;
        tx.commit().await?;
        Ok(Some(row))
    }

    pub async fn get_waiting(&self, id: &str) -> Result<Option<WaitingItem>, DbError> {
        Ok(sqlx::query_as::<_, WaitingItem>("SELECT * FROM waiting_on_person WHERE id = ?").bind(id).fetch_optional(&self.pool).await?)
    }

    /// Still waiting (open or reminded), oldest first; `space` keeps that space's bots.
    pub async fn list_waiting(&self, space: Option<&super::spaces::SpaceId>) -> Result<Vec<WaitingItem>, DbError> {
        Ok(sqlx::query_as::<_, WaitingItem>(
            "SELECT * FROM waiting_on_person WHERE status != 'closed'
               AND (?1 IS NULL OR COALESCE((SELECT k.space_id FROM kwants k WHERE k.id = waiting_on_person.kwant_id), 'spc_default') = ?1)
             ORDER BY created_at LIMIT 200",
        )
        .bind(space.map(|s| s.as_str()))
        .fetch_all(&self.pool)
        .await?)
    }

    /// The question was answered or dismissed. Returns the items it closed (as every close
    /// below does), so the caller can say so live (`server::waiting::closed`).
    pub async fn close_waiting_for_question(&self, question_id: &str) -> Result<Vec<WaitingItem>, DbError> {
        Ok(sqlx::query_as::<_, WaitingItem>("UPDATE waiting_on_person SET status = 'closed' WHERE question_id = ? AND status != 'closed' RETURNING *")
            .bind(question_id)
            .fetch_all(&self.pool)
            .await?)
    }

    /// The person wrote in a chat: a thread (`thread_id`), a channel's main chat, or the direct
    /// chat with a bot (`direct_kwant_id`, no channel). Everything waiting there ends.
    pub async fn close_waiting_in_chat(
        &self,
        channel_id: Option<&str>,
        direct_kwant_id: Option<&str>,
        thread_id: Option<&str>,
    ) -> Result<Vec<WaitingItem>, DbError> {
        let (sql, id) = match (thread_id, channel_id, direct_kwant_id) {
            (Some(t), _, _) => ("UPDATE waiting_on_person SET status = 'closed' WHERE status != 'closed' AND thread_id = ? RETURNING *", t),
            (None, Some(c), _) => (
                "UPDATE waiting_on_person SET status = 'closed' WHERE status != 'closed' AND channel_id = ? AND thread_id IS NULL RETURNING *",
                c,
            ),
            (None, None, Some(k)) => (
                "UPDATE waiting_on_person SET status = 'closed' WHERE status != 'closed' AND channel_id IS NULL \
                 AND thread_id IS NULL AND direct_kwant_id = ? RETURNING *",
                k,
            ),
            (None, None, None) => return Ok(Vec::new()),
        };
        Ok(sqlx::query_as::<_, WaitingItem>(sql).bind(id).fetch_all(&self.pool).await?)
    }

    /// A bot's plain "waiting on a reply" entries in one chat (no question card) end: the ask
    /// became a card, which has its own entry.
    pub async fn close_plain_waiting(
        &self,
        kwant_id: &str,
        channel_id: Option<&str>,
        direct_kwant_id: Option<&str>,
        thread_id: Option<&str>,
    ) -> Result<Vec<WaitingItem>, DbError> {
        Ok(sqlx::query_as::<_, WaitingItem>(&format!(
            "UPDATE waiting_on_person SET status = 'closed' WHERE question_id IS NULL AND kind IS NULL AND status != 'closed' AND {SAME_CHAT} RETURNING *"
        ))
        .bind(kwant_id)
        .bind(channel_id.unwrap_or(""))
        .bind(direct_kwant_id.unwrap_or(""))
        .bind(thread_id.unwrap_or(""))
        .fetch_all(&self.pool)
        .await?)
    }

    /// Records that a bot's work stopped in one chat: its earlier stopped item and its "waiting
    /// on a reply" item there close (this says more), and one new item opens. Never reminded.
    /// Returns the new item and the ones it replaced.
    #[allow(clippy::too_many_arguments)]
    pub async fn add_stopped(
        &self,
        kwant_id: &str,
        channel_id: Option<&str>,
        direct_kwant_id: Option<&str>,
        thread_id: Option<&str>,
        message_id: Option<&str>,
        line: &str,
        retry: Option<&str>,
    ) -> Result<(WaitingItem, Vec<WaitingItem>), DbError> {
        let mut tx = self.begin_write().await?;
        let replaced = sqlx::query_as::<_, WaitingItem>(&format!(
            "UPDATE waiting_on_person SET status = 'closed' WHERE question_id IS NULL AND status != 'closed' AND {SAME_CHAT} RETURNING *"
        ))
        .bind(kwant_id)
        .bind(channel_id.unwrap_or(""))
        .bind(direct_kwant_id.unwrap_or(""))
        .bind(thread_id.unwrap_or(""))
        .fetch_all(&mut *tx)
        .await?;
        let row = sqlx::query_as::<_, WaitingItem>(
            "INSERT INTO waiting_on_person (id, kwant_id, channel_id, direct_kwant_id, thread_id, message_id, question, status, created_at, kind, retry) \
             VALUES (?, ?, ?, ?, ?, ?, ?, 'open', ?, ?, ?) RETURNING *",
        )
        .bind(Uuid::new_v4().to_string())
        .bind(kwant_id)
        .bind(channel_id)
        .bind(direct_kwant_id)
        .bind(thread_id)
        .bind(message_id)
        .bind(line)
        .bind(Utc::now())
        .bind(STOPPED)
        .bind(retry)
        .fetch_one(&mut *tx)
        .await?;
        tx.commit().await?;
        Ok((row, replaced))
    }

    /// The bot's work in that chat went on (a later reply finished): its stopped items there close.
    pub async fn close_stopped(
        &self,
        kwant_id: &str,
        channel_id: Option<&str>,
        direct_kwant_id: Option<&str>,
        thread_id: Option<&str>,
    ) -> Result<Vec<WaitingItem>, DbError> {
        Ok(sqlx::query_as::<_, WaitingItem>(&format!(
            "UPDATE waiting_on_person SET status = 'closed' WHERE kind = 'stopped' AND status != 'closed' AND {SAME_CHAT} RETURNING *"
        ))
        .bind(kwant_id)
        .bind(channel_id.unwrap_or(""))
        .bind(direct_kwant_id.unwrap_or(""))
        .bind(thread_id.unwrap_or(""))
        .fetch_all(&self.pool)
        .await?)
    }

    /// Closes one item if it is still open; `None` when it was already closed or never existed
    /// (a second Retry or Dismiss).
    pub async fn claim_waiting(&self, id: &str) -> Result<Option<WaitingItem>, DbError> {
        Ok(sqlx::query_as::<_, WaitingItem>("UPDATE waiting_on_person SET status = 'closed' WHERE id = ? AND status != 'closed' RETURNING *")
            .bind(id)
            .fetch_optional(&self.pool)
            .await?)
    }

    /// Stopped items still open whose notice is `message_id` (a chain notice's Continue).
    pub async fn close_waiting_for_message(&self, message_id: &str) -> Result<Vec<WaitingItem>, DbError> {
        Ok(sqlx::query_as::<_, WaitingItem>(
            "UPDATE waiting_on_person SET status = 'closed' WHERE message_id = ? AND kind = 'stopped' AND status != 'closed' RETURNING *",
        )
        .bind(message_id)
        .fetch_all(&self.pool)
        .await?)
    }

    /// The stopped item whose Retry asks again for approval card `approval_id`.
    pub async fn close_stopped_for_approval(&self, approval_id: &str) -> Result<Vec<WaitingItem>, DbError> {
        Ok(sqlx::query_as::<_, WaitingItem>(
            "UPDATE waiting_on_person SET status = 'closed' WHERE kind = 'stopped' AND status != 'closed' \
             AND json_extract(retry, '$.approval_id') = ? RETURNING *",
        )
        .bind(approval_id)
        .fetch_all(&self.pool)
        .await?)
    }

    /// 22i: open items whose place is gone, so nobody can see or resume them: the bot was
    /// deleted, or the channel or thread they were asked in no longer exists. The caller closes
    /// them ([`Self::claim_waiting`]); `space` limits it like [`Self::list_waiting`].
    pub async fn waiting_places_gone(&self, space: Option<&super::spaces::SpaceId>) -> Result<Vec<WaitingItem>, DbError> {
        Ok(sqlx::query_as::<_, WaitingItem>(
            "SELECT * FROM waiting_on_person w WHERE w.status != 'closed'
               AND (?1 IS NULL OR COALESCE((SELECT k.space_id FROM kwants k WHERE k.id = w.kwant_id), 'spc_default') = ?1)
               AND (NOT EXISTS (SELECT 1 FROM kwants k WHERE k.id = w.kwant_id)
                    OR (w.channel_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM channels c WHERE c.id = w.channel_id))
                    OR (w.thread_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM threads t WHERE t.id = w.thread_id))
                    OR (w.channel_id IS NULL AND w.thread_id IS NULL AND w.direct_kwant_id IS NOT NULL
                        AND NOT EXISTS (SELECT 1 FROM kwants k WHERE k.id = w.direct_kwant_id)))
             ORDER BY w.created_at",
        )
        .bind(space.map(|s| s.as_str()))
        .fetch_all(&self.pool)
        .await?)
    }

    /// The entries whose reminder is due by `now`, each claimed (`open` → `reminded`) so it goes
    /// out once, whoever else is looking.
    pub async fn take_due_waiting(&self, now: DateTime<Utc>) -> Result<Vec<WaitingItem>, DbError> {
        let due = sqlx::query_as::<_, WaitingItem>(
            "SELECT * FROM waiting_on_person WHERE status = 'open' AND due_at IS NOT NULL AND due_at <= ? ORDER BY due_at",
        )
        .bind(now)
        .fetch_all(&self.pool)
        .await?;
        let mut claimed = Vec::new();
        for w in due {
            let res = sqlx::query("UPDATE waiting_on_person SET status = 'reminded', reminded_at = ? WHERE id = ? AND status = 'open'")
                .bind(now)
                .bind(&w.id)
                .execute(&self.pool)
                .await?;
            if res.rows_affected() > 0 {
                claimed.push(WaitingItem { status: "reminded".into(), reminded_at: Some(now), ..w });
            }
        }
        Ok(claimed)
    }

    pub async fn next_waiting_due(&self) -> Result<Option<DateTime<Utc>>, DbError> {
        let row: Option<(DateTime<Utc>,)> = sqlx::query_as(
            "SELECT due_at FROM waiting_on_person WHERE status = 'open' AND due_at IS NOT NULL ORDER BY due_at LIMIT 1",
        )
        .fetch_optional(&self.pool)
        .await?;
        Ok(row.map(|(at,)| at))
    }

    /// Reminders were switched off: what is still waiting stays listed but is never reminded.
    pub async fn clear_waiting_due(&self) -> Result<u64, DbError> {
        let res = sqlx::query("UPDATE waiting_on_person SET due_at = NULL WHERE status = 'open' AND due_at IS NOT NULL")
            .execute(&self.pool)
            .await?;
        Ok(res.rows_affected())
    }
}

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

    fn item(kwant: &str, channel: Option<&str>, direct: Option<&str>, thread: Option<&str>, q: Option<&str>) -> NewWaitingItem {
        NewWaitingItem {
            kwant_id: kwant.into(),
            channel_id: channel.map(Into::into),
            direct_kwant_id: direct.map(Into::into),
            thread_id: thread.map(Into::into),
            question_id: q.map(Into::into),
            message_id: None,
            question: "Which one?".into(),
            due_at: Some(Utc::now() - Duration::seconds(1)),
        }
    }

    #[tokio::test]
    async fn one_entry_per_question_and_one_reminder() {
        let tmp = tempfile::tempdir().unwrap();
        let db = Database::init(tmp.path()).await.unwrap();
        assert!(db.add_waiting(&item("k", None, Some("k"), None, Some("q1"))).await.unwrap().is_some());
        assert!(db.add_waiting(&item("k", None, Some("k"), None, Some("q1"))).await.unwrap().is_none());
        // A plain ask in the same chat while one is waiting keeps the first
        assert!(db.add_waiting(&item("k", None, Some("k"), None, None)).await.unwrap().is_some());
        assert!(db.add_waiting(&item("k", None, Some("k"), None, None)).await.unwrap().is_none());
        assert_eq!(db.take_due_waiting(Utc::now()).await.unwrap().len(), 2);
        assert!(db.take_due_waiting(Utc::now()).await.unwrap().is_empty());
        assert_eq!(db.next_waiting_due().await.unwrap(), None);
        // Reminded entries are still listed until the person writes
        assert_eq!(db.list_waiting(None).await.unwrap().len(), 2);
        assert_eq!(db.close_waiting_in_chat(None, Some("k"), None).await.unwrap().len(), 2);
        assert!(db.list_waiting(None).await.unwrap().is_empty());
        // A closed question is never recorded again
        assert!(db.add_waiting(&item("k", None, Some("k"), None, Some("q1"))).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn writing_closes_only_that_chat() {
        let tmp = tempfile::tempdir().unwrap();
        let db = Database::init(tmp.path()).await.unwrap();
        db.add_waiting(&item("a", Some("c1"), None, None, None)).await.unwrap();
        db.add_waiting(&item("b", Some("c1"), None, Some("t1"), None)).await.unwrap();
        db.add_waiting(&item("a", None, Some("a"), None, None)).await.unwrap();
        db.add_waiting(&item("b", Some("c2"), None, None, Some("q9"))).await.unwrap();
        assert_eq!(db.close_waiting_in_chat(Some("c1"), None, None).await.unwrap().len(), 1);
        assert_eq!(db.close_waiting_in_chat(None, None, Some("t1")).await.unwrap().len(), 1);
        assert_eq!(db.close_waiting_for_question("q9").await.unwrap().len(), 1);
        let left = db.list_waiting(None).await.unwrap();
        assert_eq!(left.len(), 1);
        assert_eq!(left[0].direct_kwant_id.as_deref(), Some("a"));
        assert_eq!(db.clear_waiting_due().await.unwrap(), 1);
        assert!(db.take_due_waiting(Utc::now()).await.unwrap().is_empty());
    }

    #[tokio::test]
    async fn a_stopped_item_replaces_the_chats_ask_and_closes_once() {
        let tmp = tempfile::tempdir().unwrap();
        let db = Database::init(tmp.path()).await.unwrap();
        // A "waiting on your reply" item in the chat, then the work stops there
        db.add_waiting(&item("k", None, Some("k"), None, None)).await.unwrap();
        let a = db.add_stopped("k", None, Some("k"), None, Some("m1"), "Stopped. Retry in the chat sends your message again.", None).await.unwrap().0;
        assert_eq!(a.kind.as_deref(), Some(STOPPED));
        let open = db.list_waiting(None).await.unwrap();
        assert_eq!(open.len(), 1, "the stop says more than the plain ask: {open:?}");
        // A second stop in the same chat replaces the first: one ask per chat
        let b = db.add_stopped("k", None, Some("k"), None, Some("m2"), "Again", None).await.unwrap().0;
        let open = db.list_waiting(None).await.unwrap();
        assert_eq!(open.iter().map(|w| w.id.as_str()).collect::<Vec<_>>(), vec![b.id.as_str()]);
        // Never reminded
        assert!(db.take_due_waiting(Utc::now() + Duration::days(30)).await.unwrap().is_empty());
        // A stop elsewhere is untouched; a later reply of the bot there closes only that chat's
        db.add_stopped("k", Some("c1"), None, None, None, "In the channel", None).await.unwrap();
        assert_eq!(db.close_stopped("k", None, Some("k"), None).await.unwrap().len(), 1);
        assert_eq!(db.list_waiting(None).await.unwrap().len(), 1);
        // Plain closes don't take a stopped item with them, a write does
        db.add_waiting(&item("k", Some("c1"), None, None, None)).await.unwrap();
        assert_eq!(db.close_waiting_in_chat(Some("c1"), None, None).await.unwrap().len(), 2);
        assert!(db.list_waiting(None).await.unwrap().is_empty());
    }

    #[tokio::test]
    async fn a_stopped_item_closes_by_its_notice_or_its_approval_once() {
        let tmp = tempfile::tempdir().unwrap();
        let db = Database::init(tmp.path()).await.unwrap();
        let cont = db.add_stopped("k", Some("c1"), None, None, Some("notice1"), "Paused", Some(r#"{"type":"continue"}"#)).await.unwrap().0;
        assert_eq!(cont.retry.as_deref(), Some(r#"{"type":"continue"}"#));
        db.add_stopped("k", Some("c2"), None, None, Some("notice2"), "Expired", Some(r#"{"type":"approval","approval_id":"ap1"}"#)).await.unwrap();
        assert_eq!(db.close_waiting_for_message("notice1").await.unwrap().len(), 1);
        assert_eq!(db.close_waiting_for_message("notice1").await.unwrap().len(), 0);
        assert_eq!(db.close_stopped_for_approval("ap2").await.unwrap().len(), 0);
        assert_eq!(db.close_stopped_for_approval("ap1").await.unwrap().len(), 1);
        assert!(db.list_waiting(None).await.unwrap().is_empty());
        let c = db.add_stopped("k", Some("c3"), None, None, None, "x", None).await.unwrap().0;
        assert!(db.claim_waiting(&c.id).await.unwrap().is_some());
        assert!(db.claim_waiting(&c.id).await.unwrap().is_none(), "a second Retry gets nothing");
    }

    async fn bot(db: &Database, id: &str) {
        sqlx::query("INSERT INTO kwants (id, name, title, persona_prompt, avatar_sprite, space_id) VALUES (?, ?, 't', 'p', 'a', 'spc_default')")
            .bind(id)
            .bind(format!("Bot {id}"))
            .execute(&db.pool)
            .await
            .unwrap();
    }

    /// 22i: an item asked in a chat that no longer exists (bot, channel or thread deleted) is
    /// found so it can close, and deleting a bot closes its items itself.
    #[tokio::test]
    async fn items_whose_place_is_gone_are_found_and_a_deleted_bot_closes_its_own() {
        let tmp = tempfile::tempdir().unwrap();
        let db = Database::init(tmp.path()).await.unwrap();
        bot(&db, "k1").await;
        bot(&db, "k2").await;
        sqlx::query("INSERT INTO channels (id, name, description, lead_kwant_id, member_kwants, routing_mode, space_id) VALUES ('c1', 'ops', '', 'k1', '[\"k1\"]', 'auto', 'spc_default')")
            .execute(&db.pool)
            .await
            .unwrap();
        let t1 = db.create_thread("k1", "Fix-it: repo #1", "chat", None, "kwant").await.unwrap();
        // Places that exist
        db.add_stopped("k1", None, Some("k1"), None, Some("m1"), "direct", None).await.unwrap();
        db.add_stopped("k1", Some("c1"), None, None, Some("m2"), "channel", None).await.unwrap();
        db.add_stopped("k1", None, None, Some(&t1.id), Some("m3"), "thread", None).await.unwrap();
        assert!(db.waiting_places_gone(None).await.unwrap().is_empty());
        // Places that don't: a channel and a thread that were deleted, a bot that never was
        db.add_stopped("k1", Some("c-gone"), None, None, Some("m4"), "gone channel", None).await.unwrap();
        db.add_stopped("k1", None, None, Some("t-gone"), Some("m5"), "gone thread", None).await.unwrap();
        db.add_stopped("k-gone", None, Some("k-gone"), None, Some("m6"), "gone bot", None).await.unwrap();
        let gone: Vec<String> = db.waiting_places_gone(None).await.unwrap().into_iter().map(|w| w.question).collect();
        assert_eq!(gone, vec!["gone channel", "gone thread", "gone bot"]);
        for w in db.waiting_places_gone(None).await.unwrap() {
            assert!(db.claim_waiting(&w.id).await.unwrap().is_some());
        }
        assert!(db.waiting_places_gone(None).await.unwrap().is_empty());
        assert_eq!(db.list_waiting(None).await.unwrap().len(), 3);
        // Deleting k1 closes its three (direct chat, channel ask, its thread) in the same transaction
        db.add_stopped("k2", None, Some("k2"), None, Some("m7"), "k2's own", None).await.unwrap();
        assert!(db.delete_kwant_and_history("k1", "k2").await.unwrap());
        let left: Vec<String> = db.list_waiting(None).await.unwrap().into_iter().map(|w| w.question).collect();
        assert_eq!(left, vec!["k2's own"]);
    }
}