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.

41.0 KB
Raw Blame
//! Bringing bots in from other apps, and taking them out again.
//!
//! Every source (a Claude Code agent, a character card, a Grok Bot bundle, an OpenMausBot
//! package, a pasted export-prompt reply, …) is first turned into one [`Bundle`] in memory.
//! Only that bundle is checked ([`normalize`]), previewed and applied; the upload itself is never
//! stored. See `IMPORT-API.md` in the onboarding notes for the HTTP side.

pub mod archive;
pub mod github;
pub mod local;
pub mod parsers;
pub mod prompts;
pub mod scrub;

use crate::db::continuity::{clean_memory_text, MAX_INSTRUCTIONS_CHARS, MAX_MEMORIES_PER_KWANT, MAX_MEMORY_CHARS};
use crate::db::routing::{MAX_SCOPE_ENTRIES, MAX_SCOPE_ENTRY_CHARS};
use crate::db::skills::{clean_skill_name, clean_skill_text, MAX_PENDING_PER_KWANT, MAX_SKILL_DESCRIPTION_CHARS};
use crate::routines::Schedule;
use crate::sandbox::site_guard;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

pub const BUNDLE_FORMAT: &str = "kwant.bundle/1";
/// `source.kind` of pasted text that was taken as one bot's instructions.
pub const PLAIN_TEXT_KIND: &str = "plain-text";

/// Pasted text (an export-prompt reply).
pub const MAX_TEXT_BYTES: usize = 256 * 1024;
/// One JSON, Markdown or PNG file.
pub const MAX_FILE_BYTES: usize = 4 * 1024 * 1024;
/// A zip as uploaded.
pub const MAX_ZIP_BYTES: usize = 20 * 1024 * 1024;
/// Everything read out of one zip, counted as it is inflated.
pub const MAX_UNZIPPED_BYTES: u64 = 50 * 1024 * 1024;
pub const MAX_ZIP_ENTRIES: usize = 500;
pub const MAX_BOTS: usize = 50;
pub const MAX_TEAMS: usize = 10;
/// Instructions (`persona_prompt`) and `soul`, here and on every bot create and edit.
pub const MAX_PERSONA_CHARS: usize = 16_000;
pub const MAX_NAME_CHARS: usize = 40;
pub const MAX_TITLE_CHARS: usize = 100;
pub const MAX_ROUTINE_NAME_CHARS: usize = 100;
pub const MAX_ROUTINES_PER_BOT: usize = 20;
pub const MAX_STARTERS: usize = 10;
pub const MAX_STARTER_CHARS: usize = 1_000;
pub const MAX_HINTS: usize = 50;
pub const MAX_HINT_CHARS: usize = 200;
pub const MAX_SHARED_SKILLS: usize = 50;
pub const MAX_TEAM_NAME_CHARS: usize = 60;

/// The message every create and edit path gives for an over-long persona or soul.
pub fn persona_too_long(field: &str) -> String {
    format!("{field} can be at most 16,000 characters")
}

/// `None` when instructions and soul fit [`MAX_PERSONA_CHARS`], else the error to show.
pub fn check_persona_caps(persona_prompt: &str, soul: &str) -> Option<String> {
    if persona_prompt.chars().count() > MAX_PERSONA_CHARS {
        return Some(persona_too_long("instructions"));
    }
    if soul.chars().count() > MAX_PERSONA_CHARS {
        return Some(persona_too_long("soul"));
    }
    None
}

/// `kwant.bundle/1`: bots, their memories, skills and routines, and teams, from anywhere.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Bundle {
    pub format: String,
    #[serde(default)]
    pub source: Source,
    #[serde(default)]
    pub bots: Vec<BotSpec>,
    #[serde(default)]
    pub teams: Vec<TeamSpec>,
    /// Skills not tied to one bot; they become the space's shared skills.
    #[serde(default)]
    pub skills: Vec<SkillSpec>,
    /// What the source had that kwant doesn't import, in words.
    #[serde(default)]
    pub omitted: Vec<String>,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Source {
    /// `kwant`, `claude-code`, `skill`, `character-card`, `grokbot`, `openmaus`, `openmaus-grok`,
    /// `prompt-reply`, `plain-text`, or what an export-prompt reply says (`chatgpt`, `grok`, …).
    #[serde(default)]
    pub kind: String,
    #[serde(default)]
    pub label: Option<String>,
    #[serde(default)]
    pub exported_at: Option<String>,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct BotSpec {
    /// The bot's id within the bundle; teams name bots by it.
    #[serde(rename = "ref", default)]
    pub ref_: String,
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub title: String,
    /// The bot's instructions.
    #[serde(default)]
    pub persona_prompt: String,
    #[serde(default)]
    pub soul: String,
    #[serde(default)]
    pub avatar_hint: Option<String>,
    #[serde(default)]
    pub model_hint: Option<String>,
    /// Conversation starters: shown in the preview; kwant has no field for them yet.
    #[serde(default)]
    pub starters: Vec<String>,
    #[serde(default)]
    pub memories: Vec<String>,
    #[serde(default)]
    pub skills: Vec<SkillSpec>,
    #[serde(default)]
    pub routines: Vec<RoutineSpec>,
    /// Tools the bot used: shown, never connected.
    #[serde(default)]
    pub tool_hints: Vec<String>,
    /// Files the bot had: "not imported, re-attach these".
    #[serde(default)]
    pub knowledge_files: Vec<String>,
    /// Which sites the bot's browser may open (see [`crate::sandbox::site_guard`]); kept so a
    /// bot never loses its limits on the way out and back in.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub browser_allowed_sites: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub browser_blocked_sites: Vec<String>,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub browser_block_private_network: bool,
    /// What the bot is for and not for ("Good for" / "Not for"; see [`crate::db::routing::Scope`]),
    /// kept so a copied bot is routed the same way in channels.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub covers: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub avoids: Vec<String>,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SkillSpec {
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub body: String,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct RoutineSpec {
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub instructions: String,
    /// A kwant [`Schedule`] as JSON; when absent or invalid, `schedule_text` is read.
    #[serde(default)]
    pub schedule: Option<serde_json::Value>,
    /// The schedule as the source wrote it: a cron line, `09:00`, `daily 09:00`, ….
    #[serde(default)]
    pub schedule_text: Option<String>,
    /// Each run's Flight School class (kwant bundles only; absent: `general`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub task_class: Option<String>,
    /// What "done" means for each run (kwant bundles only).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub acceptance: Option<String>,
    /// Each run's checks `[{kind, spec, required}]`. Exported only when no spec holds a secret;
    /// never applied on import, since a check runs commands the person must approve.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub checks: Option<Vec<serde_json::Value>>,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct TeamSpec {
    #[serde(default)]
    pub name: String,
    /// A bot `ref`.
    #[serde(default)]
    pub lead: Option<String>,
    /// Bot `ref`s.
    #[serde(default)]
    pub members: Vec<String>,
    #[serde(default)]
    pub instructions: Option<String>,
}

/// Something the preview tells the person, about one bot (`bot` = its ref) or the whole import.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Notice {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bot: Option<String>,
    pub message: String,
}

impl Notice {
    fn new(bot: Option<&str>, message: impl Into<String>) -> Self {
        Notice { bot: bot.map(str::to_string), message: message.into() }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Rename {
    #[serde(rename = "ref")]
    pub ref_: String,
    pub from: String,
    pub to: String,
}

/// A checked bundle and what the person should know before applying it.
#[derive(Debug, Clone, Serialize)]
pub struct Checked {
    pub bundle: Bundle,
    /// Kept, but worth a look (a removed key, a suspicious line, a routine with no schedule, …).
    pub warnings: Vec<Notice>,
    /// Not imported (too long, too many, invalid).
    pub dropped: Vec<Notice>,
    /// Stop the import until fixed (instructions over 16,000 characters, too many bots).
    pub problems: Vec<Notice>,
    /// Bots that get a new name because the space (or the bundle) already has theirs.
    pub renames: Vec<Rename>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ImportError {
    /// 413
    TooLarge(String),
    /// 415: not a format kwant reads.
    Unsupported(String),
    /// 422: a known format with bad content, or an unsafe archive.
    Invalid(String),
}

impl std::fmt::Display for ImportError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ImportError::TooLarge(m) | ImportError::Unsupported(m) | ImportError::Invalid(m) => f.write_str(m),
        }
    }
}

impl std::error::Error for ImportError {}

/// Cuts `s` to at most `max` characters.
pub(crate) fn cut(s: &str, max: usize) -> String {
    s.chars().take(max).collect()
}

/// Control characters, and characters that draw nothing or turn text around (zero-width
/// spaces, U+202E right-to-left override and the other bidi controls, soft hyphens).
pub(crate) fn is_hidden(c: char) -> bool {
    c.is_control()
        || matches!(c, '\u{00AD}' | '\u{061C}' | '\u{180E}' | '\u{200B}'..='\u{200F}' | '\u{202A}'..='\u{202E}'
            | '\u{2060}'..='\u{2064}' | '\u{2066}'..='\u{2069}' | '\u{FEFF}' | '\u{FFF9}'..='\u{FFFB}')
}

/// A one-line label (title, team or routine name): whitespace collapsed, hidden characters gone.
fn one_line(raw: &str, max: usize) -> String {
    let visible: String = raw.chars().map(|c| if c.is_whitespace() { ' ' } else { c }).filter(|c| !is_hidden(*c)).collect();
    cut(visible.split_whitespace().collect::<Vec<_>>().join(" ").as_str(), max)
}

/// A bot name kwant accepts: no spaces, `@` or hidden characters, at most [`MAX_NAME_CHARS`].
pub fn clean_bot_name(raw: &str) -> String {
    let joined = raw.split_whitespace().collect::<Vec<_>>().join("-");
    let name: String = joined.chars().filter(|c| *c != '@' && !is_hidden(*c)).collect();
    let name = cut(name.trim_matches('-'), MAX_NAME_CHARS);
    if name.is_empty() {
        "Imported-bot".into()
    } else {
        name
    }
}

/// `name`, or `name-2`, `name-3`, … : the first one nobody in `taken` (lowercase) has.
fn free_name(name: &str, taken: &HashSet<String>) -> String {
    if !taken.contains(&name.to_lowercase()) {
        return name.to_string();
    }
    (2..)
        .map(|n| {
            let suffix = format!("-{n}");
            format!("{}{suffix}", cut(name, MAX_NAME_CHARS - suffix.len()))
        })
        .find(|c| !taken.contains(&c.to_lowercase()))
        .unwrap_or_else(|| name.to_string())
}

/// Splits a memory into entries of at most [`MAX_MEMORY_CHARS`]: at sentence ends when it can,
/// else at spaces, else anywhere. Whitespace is collapsed as stored memories are.
pub fn split_memory(text: &str) -> Vec<String> {
    let text = text.split_whitespace().collect::<Vec<_>>().join(" ");
    let mut out = Vec::new();
    let mut rest = text.as_str();
    while rest.chars().count() > MAX_MEMORY_CHARS {
        let limit = rest.char_indices().nth(MAX_MEMORY_CHARS).map(|(i, _)| i).unwrap_or(rest.len());
        let head = &rest[..limit];
        let at = head
            .rmatch_indices([' '])
            .map(|(i, _)| i)
            .find(|&i| i > 0 && head[..i].ends_with(['.', '!', '?']))
            .or_else(|| head.rfind(' ').filter(|&i| i > 0))
            .unwrap_or(limit);
        out.push(rest[..at].trim().to_string());
        rest = rest[at..].trim_start();
    }
    if !rest.is_empty() {
        out.push(rest.to_string());
    }
    out.retain(|s| !s.is_empty());
    out
}

/// Reads a routine's schedule: kwant JSON first, then the source's text. Imported routines
/// always arrive paused, so a schedule only has to be valid, not right. The second value is a
/// note for the preview when part of it (so far only the time zone) had to be guessed.
pub fn read_schedule(spec: &RoutineSpec) -> Option<(Schedule, Option<String>)> {
    match &spec.schedule {
        Some(serde_json::Value::String(t)) => {
            if let Some(found) = schedule_from_text(t) {
                return Some(found);
            }
        }
        Some(v @ serde_json::Value::Object(o)) => {
            let mut v = v.clone();
            // OpenMausBot and other apps write `timeZone`; kwant writes `tz`.
            if !o.contains_key("tz") {
                if let Some(zone) = o.get("timeZone").or_else(|| o.get("time_zone")).or_else(|| o.get("timezone")).cloned() {
                    v["tz"] = zone;
                }
            }
            if let Ok(s) = serde_json::from_value::<Schedule>(v) {
                if s.validate().is_ok() {
                    return Some((s.canonical(), None));
                }
                // A good expression or time with a zone kwant doesn't know: keep it in UTC.
                if let Some((zone, utc)) = with_utc(&s) {
                    if utc.validate().is_ok() {
                        return Some((utc.canonical(), Some(zone_note(&zone))));
                    }
                }
            }
        }
        _ => {}
    }
    let text = spec.schedule_text.as_deref()?.trim();
    schedule_from_text(text)
}

/// The schedule moved to UTC, with the zone it had, when it has a zone at all.
fn with_utc(s: &Schedule) -> Option<(String, Schedule)> {
    let utc = "UTC".to_string();
    Some(match s.clone() {
        Schedule::Daily { time, tz } => (tz, Schedule::Daily { time, tz: utc }),
        Schedule::Weekly { time, weekdays, tz } => (tz, Schedule::Weekly { time, weekdays, tz: utc }),
        Schedule::Monthly { time, days, tz } => (tz, Schedule::Monthly { time, days, tz: utc }),
        Schedule::Cron { expression, tz } => (tz, Schedule::Cron { expression, tz: utc }),
        Schedule::Once { .. } | Schedule::Interval { .. } => return None,
    })
}

fn zone_note(zone: &str) -> String {
    let zone = one_line(zone, 64);
    if zone.is_empty() {
        "no time zone was given, so we used UTC. Check the time before turning it on.".into()
    } else {
        format!("we don't know the time zone \"{zone}\", so we used UTC. Check the time before turning it on.")
    }
}

fn schedule_from_text(text: &str) -> Option<(Schedule, Option<String>)> {
    let (rest, zone) = split_zone(text);
    let (tz, note) = match zone {
        None => ("UTC".to_string(), None),
        Some(z) => {
            let probe = Schedule::Daily { time: "00:00".into(), tz: z.clone() };
            if probe.validate().is_ok() {
                (z, None)
            } else {
                ("UTC".to_string(), Some(zone_note(&z)))
            }
        }
    };
    let lower = rest.to_lowercase();
    let lower = lower.trim();
    // "daily 09:00", "every day at 9:00", "09:00"
    let time = lower
        .trim_start_matches("every day")
        .trim_start_matches("daily")
        .trim_start_matches(" at")
        .trim();
    if let Some(t) = clock(time) {
        return Some((Schedule::Daily { time: t, tz }.canonical(), note));
    }
    let fields: Vec<&str> = rest.split_whitespace().collect();
    if fields.len() == 5 {
        let s = Schedule::Cron { expression: fields.join(" "), tz };
        if s.validate().is_ok() {
            return Some((s.canonical(), note));
        }
    }
    None
}

/// Takes a time zone out of a schedule line: `0 7 * * * (Europe/Berlin)`, `0 7 * * * Europe/Berlin`,
/// `tz=Europe/Berlin`, `TZ: Europe/Berlin`, `CRON_TZ=Europe/Berlin 0 7 * * *`. Returns the rest
/// of the line and the zone as written; whether the zone exists is checked by the caller.
fn split_zone(text: &str) -> (String, Option<String>) {
    let tokens: Vec<&str> = text.split_whitespace().collect();
    let mut rest = Vec::new();
    let mut zone = None;
    let mut i = 0;
    while i < tokens.len() {
        let tok = tokens[i];
        let bare = tok.trim_matches(|c| "()[],;".contains(c));
        let lower = bare.to_ascii_lowercase();
        let labelled = ["cron_tz", "tz", "timezone", "time_zone"].iter().find_map(|label| {
            let after = lower.strip_prefix(label)?;
            let sep = after.chars().next()?;
            ("=:".contains(sep)).then(|| bare[label.len() + 1..].to_string())
        });
        if let Some(value) = labelled {
            if value.is_empty() {
                // "TZ: Europe/Berlin": the value is the next word
                if let Some(next) = tokens.get(i + 1) {
                    zone = Some(next.trim_matches(|c| "()[],;".contains(c)).to_string());
                    i += 1;
                }
            } else {
                zone = Some(value);
            }
        } else if zone.is_none() && looks_like_zone(bare) && (tok != bare || i == tokens.len() - 1) {
            // In brackets anywhere, or the last word
            zone = Some(bare.to_string());
        } else if zone.is_none() && tok.starts_with('(') && (lower.starts_with("utc") || lower.starts_with("gmt")) {
            // "(UTC+2)": an offset kwant can't store as a zone, so the caller falls back with a note
            zone = Some(bare.to_string());
        } else {
            rest.push(tok.trim_end_matches([',', ';']));
        }
        i += 1;
    }
    (rest.join(" "), zone)
}

/// `Europe/Berlin`, `America/Argentina/Buenos_Aires`, `UTC`: never a cron field like `*/5`.
fn looks_like_zone(s: &str) -> bool {
    if s.eq_ignore_ascii_case("utc") || s.eq_ignore_ascii_case("gmt") {
        return true;
    }
    let mut parts = s.split('/');
    let first = parts.next().unwrap_or("");
    s.contains('/')
        && first.len() >= 2
        && first.starts_with(|c: char| c.is_ascii_uppercase())
        && first.chars().all(|c| c.is_ascii_alphabetic())
        && parts.all(|p| !p.is_empty() && p.chars().all(|c| c.is_ascii_alphanumeric() || "_+-".contains(c)))
}

/// `H:MM` or `HH:MM` as `HH:MM`.
fn clock(s: &str) -> Option<String> {
    let (h, m) = s.split_once(':')?;
    let (h, m): (u32, u32) = (h.parse().ok()?, m.parse().ok()?);
    (h < 24 && m < 60).then(|| format!("{h:02}:{m:02}"))
}

/// What kwant puts in place of a schedule it couldn't read: the routine arrives paused anyway.
pub fn placeholder_schedule() -> Schedule {
    Schedule::Daily { time: "09:00".into(), tz: "UTC".into() }
}

/// Text that might be someone steering the bot rather than describing it. Flagged, never removed.
pub fn suspicious(text: &str) -> Vec<&'static str> {
    let lower = text.to_lowercase();
    let mut found = Vec::new();
    if lower.contains("http://") || lower.contains("https://") {
        found.push("contains a web address");
    }
    if ["ignore previous", "ignore all previous", "ignore the above", "disregard previous", "disregard all previous"]
        .iter()
        .any(|p| lower.contains(p))
    {
        found.push("tells the bot to ignore earlier instructions");
    }
    if ["send it to", "send them to", "post it to", "upload it to", "forward it to", "email it to", "exfiltrat"]
        .iter()
        .any(|p| lower.contains(p))
    {
        found.push("tells the bot to send data somewhere");
    }
    if text.contains(['\u{200b}', '\u{200c}', '\u{200d}', '\u{2060}', '\u{feff}'])
        || text.contains("                                                  ")
        || text.contains("\n\n\n\n\n\n\n\n\n\n")
    {
        found.push("has long or invisible whitespace that could hide text");
    }
    found
}

/// A bot's browser site rules in words, or `None` when it has none.
fn site_limits(bot: &BotSpec) -> Option<String> {
    let mut parts = Vec::new();
    if !bot.browser_allowed_sites.is_empty() {
        parts.push(format!("only {}", bot.browser_allowed_sites.join(", ")));
    }
    if !bot.browser_blocked_sites.is_empty() {
        parts.push(format!("never {}", bot.browser_blocked_sites.join(", ")));
    }
    if bot.browser_block_private_network {
        parts.push("no local or private network addresses".to_string());
    }
    (!parts.is_empty()).then(|| parts.join("; "))
}

/// A bot's covers/avoids lists in the app's words, or `None` when both are empty.
fn scope_words(bot: &BotSpec) -> Option<String> {
    let mut parts = Vec::new();
    if !bot.covers.is_empty() {
        parts.push(format!("Good for: {}.", bot.covers.join(", ")));
    }
    if !bot.avoids.is_empty() {
        parts.push(format!("Not for: {}.", bot.avoids.join(", ")));
    }
    (!parts.is_empty()).then(|| parts.join(" "))
}

/// A covers or avoids list as `PUT /api/kwants/:id` keeps it (trimmed, no blanks or repeats
/// ignoring case), with hidden characters and extra spaces removed. An entry that's too long or
/// past the cap is left out with a note instead of stopping the import: the lists only steer
/// routing.
fn tidy_scope(list: &[String], label: &str, who: Option<&str>, dropped: &mut Vec<Notice>) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for raw in list {
        let entry = one_line(raw, usize::MAX);
        if entry.is_empty() || out.iter().any(|o| o.eq_ignore_ascii_case(&entry)) {
            continue;
        }
        if entry.chars().count() > MAX_SCOPE_ENTRY_CHARS {
            dropped.push(Notice::new(who, format!("{label} entry over {MAX_SCOPE_ENTRY_CHARS} characters: {}…", cut(&entry, MAX_SCOPE_ENTRY_CHARS))));
        } else if out.len() == MAX_SCOPE_ENTRIES {
            dropped.push(Notice::new(who, format!("{label} entry (over the {MAX_SCOPE_ENTRIES}-entry limit): {entry}")));
        } else {
            out.push(entry);
        }
    }
    out
}

/// Checks and tidies `bundle` for a space whose bots are called `existing` (any case). Pure: the
/// same input always gives the same result, so apply re-runs it on what the preview returned.
pub fn normalize(mut bundle: Bundle, existing: &[String]) -> Checked {
    let mut warnings = Vec::new();
    let mut dropped = Vec::new();
    let mut problems = Vec::new();
    let mut renames = Vec::new();

    bundle.format = BUNDLE_FORMAT.into();
    bundle.source.kind = cut(bundle.source.kind.trim(), 40);
    bundle.source.label = bundle.source.label.as_deref().map(|l| cut(l.trim(), 200));
    bundle.source.exported_at = bundle.source.exported_at.as_deref().map(|l| cut(l.trim(), 40));

    let mut refs = HashSet::new();
    for (i, bot) in bundle.bots.iter_mut().enumerate() {
        let mut r = cut(bot.ref_.trim(), 80);
        let mut n = i + 1;
        while r.is_empty() || refs.contains(&r) {
            r = format!("bot-{n}");
            n += 1;
        }
        refs.insert(r.clone());
        bot.ref_ = r;
    }

    if bundle.source.kind == PLAIN_TEXT_KIND {
        let who = bundle.bots.first().map(|b| b.ref_.clone());
        warnings.push(Notice::new(who.as_deref(), "No name or memory found — we used the text as this bot's instructions. You can rename it after importing."));
    }

    let removed = scrub::scrub_bundle(&mut bundle);
    for (bot, count) in removed {
        let what = if count == 1 { "something that looked like an API key" } else { "things that looked like API keys" };
        warnings.push(Notice::new(bot.as_deref(), format!("We removed {what}. It shows as [removed key].")));
    }

    if bundle.bots.len() > MAX_BOTS {
        problems.push(Notice::new(None, format!("One import can bring at most {MAX_BOTS} bots; this has {}.", bundle.bots.len())));
    }
    if bundle.teams.len() > MAX_TEAMS {
        problems.push(Notice::new(None, format!("One import can bring at most {MAX_TEAMS} teams; this has {}.", bundle.teams.len())));
    }

    let mut taken: HashSet<String> = existing.iter().map(|n| n.to_lowercase()).collect();
    for bot in bundle.bots.iter_mut() {
        let r = bot.ref_.clone();
        let who = Some(r.as_str());

        let wanted = clean_bot_name(&bot.name);
        let name = free_name(&wanted, &taken);
        if name != wanted {
            renames.push(Rename { ref_: r.clone(), from: wanted.clone(), to: name.clone() });
        } else if wanted != bot.name.trim() {
            let shown: String = bot.name.trim().chars().filter(|c| !is_hidden(*c)).collect();
            warnings.push(Notice::new(
                who,
                if shown == wanted {
                    format!("We removed hidden characters from the name {wanted}.")
                } else {
                    format!("Bot names can't have spaces or @, so \"{shown}\" becomes {wanted}.")
                },
            ));
        }
        taken.insert(name.to_lowercase());
        bot.name = name;

        bot.title = one_line(&bot.title, MAX_TITLE_CHARS);
        if bot.title.is_empty() {
            bot.title = "Imported bot".into();
        }
        bot.persona_prompt = bot.persona_prompt.replace("\r\n", "\n").trim().to_string();
        bot.soul = bot.soul.replace("\r\n", "\n").trim().to_string();
        if bot.persona_prompt.is_empty() {
            warnings.push(Notice::new(who, "This bot has no instructions; it will act on its title alone until you add some."));
        }
        for (field, text) in [("instructions", &bot.persona_prompt), ("soul", &bot.soul)] {
            let n = text.chars().count();
            if n > MAX_PERSONA_CHARS {
                problems.push(Notice::new(who, format!("{} ({n} now). Shorten it where it came from (or in the pasted reply), then try the import again.", persona_too_long(&format!("{}'s {field}", bot.name)))));
            }
            for why in suspicious(text) {
                warnings.push(Notice::new(who, format!("The {field} {why}. Read it before importing.")));
            }
        }
        bot.avatar_hint = bot.avatar_hint.as_deref().map(|a| cut(a.trim(), 40)).filter(|a| !a.is_empty());
        bot.model_hint = bot.model_hint.as_deref().map(|a| cut(a.trim(), 100)).filter(|a| !a.is_empty());
        bot.starters = tidy_list(&bot.starters, MAX_STARTERS, MAX_STARTER_CHARS);
        bot.tool_hints = tidy_list(&bot.tool_hints, MAX_HINTS, MAX_HINT_CHARS);
        bot.knowledge_files = tidy_list(&bot.knowledge_files, MAX_HINTS, MAX_HINT_CHARS);
        if let Some(model) = bot.model_hint.as_deref().filter(|m| !m.eq_ignore_ascii_case("auto")) {
            warnings.push(Notice::new(who, format!("It used the model {model}; here it starts on auto. Pick a model after importing.")));
        }
        if !bot.tool_hints.is_empty() {
            warnings.push(Notice::new(who, format!("It used tools ({}). Imported bots start with no tools; connect the ones it needs.", bot.tool_hints.join(", "))));
        }
        if !bot.knowledge_files.is_empty() {
            dropped.push(Notice::new(who, format!("Files aren't brought over: {}. Attach them again if the bot needs them.", bot.knowledge_files.join(", "))));
        }
        for (field, list) in [(site_guard::ALLOWED, &mut bot.browser_allowed_sites), (site_guard::BLOCKED, &mut bot.browser_blocked_sites)] {
            match site_guard::normalize_sites(list, field) {
                Ok(sites) => *list = sites,
                Err(e) => problems.push(Notice::new(who, format!("{}'s {e}. Fix it in the file, then try the import again.", bot.name))),
            }
        }
        if let Some(limits) = site_limits(bot) {
            warnings.push(Notice::new(who, format!("Its browser is limited: {limits}. Change this in the bot's settings after importing.")));
        }
        bot.covers = tidy_scope(&bot.covers, "\"Good for\"", who, &mut dropped);
        bot.avoids = tidy_scope(&bot.avoids, "\"Not for\"", who, &mut dropped);
        if let Some(scope) = scope_words(bot) {
            warnings.push(Notice::new(who, format!("{scope} Channels use this to pick who answers; change it in the bot's settings after importing.")));
        }
        if !bot.starters.is_empty() {
            dropped.push(Notice::new(who, format!("{} conversation starter(s): kwant has no place for them yet.", bot.starters.len())));
        }

        let mut memories: Vec<String> = bot.memories.iter().flat_map(|m| split_memory(m)).collect();
        if memories.len() > bot.memories.iter().filter(|m| !m.trim().is_empty()).count() {
            warnings.push(Notice::new(who, format!("Long memories were split into entries of at most {MAX_MEMORY_CHARS} characters.")));
        }
        let cap = MAX_MEMORIES_PER_KWANT as usize;
        if memories.len() > cap {
            for m in &memories[cap..] {
                dropped.push(Notice::new(who, format!("Memory (over the {cap}-memory limit): {}", cut(m, 80))));
            }
            memories.truncate(cap);
        }
        memories.retain(|m| clean_memory_text(m).is_ok());
        bot.memories = memories;

        bot.skills = tidy_skills(std::mem::take(&mut bot.skills), MAX_PENDING_PER_KWANT as usize, who, &mut warnings, &mut dropped);
        bot.routines = tidy_routines(std::mem::take(&mut bot.routines), who, &mut warnings, &mut dropped);
    }
    bundle.skills = tidy_skills(std::mem::take(&mut bundle.skills), MAX_SHARED_SKILLS, None, &mut warnings, &mut dropped);

    let mut team_names = HashSet::new();
    let teams = std::mem::take(&mut bundle.teams);
    for mut team in teams {
        team.name = one_line(&team.name, MAX_TEAM_NAME_CHARS);
        let mut members: Vec<String> = Vec::new();
        for m in team.lead.iter().chain(team.members.iter()) {
            let m = m.trim().to_string();
            if refs.contains(&m) && !members.contains(&m) {
                members.push(m);
            } else if !refs.contains(&m) {
                warnings.push(Notice::new(None, format!("Team {}: {m} isn't a bot in this import, so it was left out.", team.name)));
            }
        }
        if team.name.is_empty() || members.is_empty() || !team_names.insert(team.name.to_lowercase()) {
            dropped.push(Notice::new(None, format!("Team \"{}\": it needs a unique name and at least one imported bot.", team.name)));
            continue;
        }
        team.lead = members.first().cloned();
        team.members = members;
        team.instructions = team.instructions.as_deref().map(str::trim).filter(|s| !s.is_empty()).map(|s| {
            if s.chars().count() > MAX_INSTRUCTIONS_CHARS {
                warnings.push(Notice::new(None, format!("Team {}'s instructions were cut to {MAX_INSTRUCTIONS_CHARS} characters.", team.name)));
            }
            cut(s, MAX_INSTRUCTIONS_CHARS)
        });
        bundle.teams.push(team);
    }

    bundle.omitted = tidy_list(&bundle.omitted, MAX_HINTS, MAX_HINT_CHARS);
    Checked { bundle, warnings, dropped, problems, renames }
}

fn tidy_list(items: &[String], max_items: usize, max_chars: usize) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for i in items {
        let i = cut(i.trim(), max_chars);
        if !i.is_empty() && !out.contains(&i) {
            out.push(i);
        }
    }
    out.truncate(max_items);
    out
}

fn tidy_skills(
    skills: Vec<SkillSpec>,
    max: usize,
    who: Option<&str>,
    warnings: &mut Vec<Notice>,
    dropped: &mut Vec<Notice>,
) -> Vec<SkillSpec> {
    let mut out: Vec<SkillSpec> = Vec::new();
    for s in skills {
        let slug = slug(&s.name);
        let description = if s.description.trim().is_empty() {
            first_line(&s.body)
        } else {
            s.description.clone()
        };
        let description = cut(description.split_whitespace().collect::<Vec<_>>().join(" ").as_str(), MAX_SKILL_DESCRIPTION_CHARS);
        let mut name = slug.clone();
        let mut n = 2;
        while out.iter().any(|o| o.name == name) {
            name = format!("{}-{n}", cut(&slug, 44));
            n += 1;
        }
        match clean_skill_text(&name, &description, &s.body) {
            Ok(text) if out.len() < max => {
                for why in suspicious(&text.body) {
                    warnings.push(Notice::new(who, format!("Skill {}: it {why}.", text.name)));
                }
                out.push(SkillSpec { name: text.name, description: text.description, body: text.body });
            }
            Ok(text) => dropped.push(Notice::new(who, format!("Skill {} (more than {max} skills).", text.name))),
            Err(e) => dropped.push(Notice::new(who, format!("Skill {}: {e}.", if s.name.trim().is_empty() { "(unnamed)" } else { s.name.trim() }))),
        }
    }
    out
}

fn tidy_routines(
    routines: Vec<RoutineSpec>,
    who: Option<&str>,
    warnings: &mut Vec<Notice>,
    dropped: &mut Vec<Notice>,
) -> Vec<RoutineSpec> {
    let mut out = Vec::new();
    for r in routines {
        let name = one_line(&r.name, MAX_ROUTINE_NAME_CHARS);
        let instructions = r.instructions.trim().to_string();
        let label = if name.is_empty() { "(unnamed)".to_string() } else { name.clone() };
        if name.is_empty() || instructions.is_empty() {
            dropped.push(Notice::new(who, format!("Routine {label}: it needs a name and instructions.")));
            continue;
        }
        if instructions.chars().count() > MAX_INSTRUCTIONS_CHARS {
            dropped.push(Notice::new(who, format!("Routine {label}: instructions are longer than {MAX_INSTRUCTIONS_CHARS} characters.")));
            continue;
        }
        if out.len() >= MAX_ROUTINES_PER_BOT {
            dropped.push(Notice::new(who, format!("Routine {label} (more than {MAX_ROUTINES_PER_BOT} routines).")));
            continue;
        }
        let schedule = match read_schedule(&r) {
            Some((s, note)) => {
                if let Some(note) = note {
                    warnings.push(Notice::new(who, format!("Routine {label}: {note}")));
                }
                s
            }
            None => {
                warnings.push(Notice::new(
                    who,
                    format!("Routine {label}: we couldn't read its schedule ({}), so it arrives paused at daily 09:00 UTC. Set the time before turning it on.", r.schedule_text.as_deref().unwrap_or("none")),
                ));
                placeholder_schedule()
            }
        };
        if r.checks.as_ref().is_some_and(|c| !c.is_empty()) {
            warnings.push(Notice::new(who, format!("Routine {label}: its checks weren't brought over. Add them in Routines if you want them.")));
        }
        let task_class = r
            .task_class
            .map(|c| c.trim().to_string())
            .filter(|c| crate::db::flight_school::valid_class(c) && c != crate::db::flight_school::DEFAULT_CLASS);
        let acceptance = r.acceptance.map(|a| cut(a.trim(), crate::server::scheduler::MAX_ACCEPTANCE_CHARS)).filter(|a| !a.is_empty());
        out.push(RoutineSpec {
            name,
            instructions,
            schedule: serde_json::to_value(&schedule).ok(),
            schedule_text: r.schedule_text.map(|t| cut(t.trim(), 200)),
            task_class,
            acceptance,
            checks: None,
        });
    }
    out
}

/// A skill name as kwant stores it, from whatever the source called it.
pub(crate) fn slug(raw: &str) -> String {
    let mut s = String::new();
    for c in raw.trim().trim_start_matches('/').to_lowercase().chars() {
        if c.is_ascii_lowercase() || c.is_ascii_digit() {
            s.push(c);
        } else if !s.ends_with('-') {
            s.push('-');
        }
    }
    let s = cut(s.trim_matches('-'), 48).trim_end_matches('-').to_string();
    match clean_skill_name(&s) {
        Ok(s) => s,
        Err(_) => "imported-skill".into(),
    }
}

/// The first non-empty line, without Markdown heading marks.
pub(crate) fn first_line(text: &str) -> String {
    text.lines()
        .map(|l| l.trim().trim_start_matches('#').trim())
        .find(|l| !l.is_empty() && *l != "---")
        .unwrap_or("")
        .to_string()
}

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

    #[test]
    fn memories_split_at_sentences_and_fit() {
        let long = "One sentence here. ".repeat(60);
        let parts = split_memory(&long);
        assert!(parts.len() > 1);
        assert!(parts.iter().all(|p| p.chars().count() <= MAX_MEMORY_CHARS && p.ends_with('.')));
        let no_spaces = "x".repeat(1200);
        let parts = split_memory(&no_spaces);
        assert_eq!(parts.len(), 3);
        assert!(parts.iter().all(|p| p.chars().count() <= MAX_MEMORY_CHARS));
    }

    #[test]
    fn names_are_cleaned_and_renamed_on_clash() {
        assert_eq!(clean_bot_name("  Code Reviewer @x "), "Code-Reviewer-x");
        assert_eq!(clean_bot_name("   "), "Imported-bot");
        // IMPORT-QA.md F5: right-to-left override, zero-width and control characters go.
        assert_eq!(clean_bot_name("Evil\u{202E}Name\u{200B}\u{7}"), "EvilName");
        assert_eq!(one_line(" Team\u{2066} A\u{202E}\tB ", 40), "Team A B");
        let taken: HashSet<String> = ["ada".into(), "ada-2".into()].into_iter().collect();
        assert_eq!(free_name("Ada", &taken), "Ada-3");
    }

    fn cron(expression: &str, tz: &str) -> Schedule {
        Schedule::Cron { expression: expression.into(), tz: tz.into() }
    }

    fn routine(schedule: Option<serde_json::Value>, text: Option<&str>) -> RoutineSpec {
        RoutineSpec { name: "r".into(), instructions: "i".into(), schedule, schedule_text: text.map(Into::into), ..Default::default() }
    }

    #[test]
    fn schedules_from_text() {
        let utc = |s| schedule_from_text(s).map(|(s, _)| s);
        assert_eq!(utc("09:30"), Some(Schedule::Daily { time: "09:30".into(), tz: "UTC".into() }));
        assert_eq!(utc("daily 7:05"), Some(Schedule::Daily { time: "07:05".into(), tz: "UTC".into() }));
        assert!(matches!(utc("0 9 * * 1-5"), Some(Schedule::Cron { .. })));
        assert_eq!(utc("*/15 9-17 * * MON-FRI"), Some(cron("*/15 9-17 * * MON-FRI", "UTC")), "cron steps aren't zones");
        assert_eq!(utc("whenever it feels right"), None);
    }

    #[test]
    fn schedule_text_time_zones() {
        let berlin = Some((cron("0 7 * * *", "Europe/Berlin"), None));
        // Alex's first real import, 2026-10-19
        assert_eq!(schedule_from_text("0 7 * * * (Europe/Berlin)"), berlin);
        assert_eq!(schedule_from_text("0 7 * * * Europe/Berlin"), berlin);
        assert_eq!(schedule_from_text("0 7 * * * [Europe/Berlin]"), berlin);
        assert_eq!(schedule_from_text("0 7 * * * tz=Europe/Berlin"), berlin);
        assert_eq!(schedule_from_text("0 7 * * *, TZ: Europe/Berlin"), berlin);
        assert_eq!(schedule_from_text("TZ:Europe/Berlin 0 7 * * *"), berlin);
        assert_eq!(schedule_from_text("CRON_TZ=Europe/Berlin 0 7 * * *"), berlin);
        assert_eq!(schedule_from_text("timezone=America/Argentina/Buenos_Aires 0 7 * * *"), Some((cron("0 7 * * *", "America/Argentina/Buenos_Aires"), None)));
        assert_eq!(schedule_from_text("0 7 * * * UTC"), Some((cron("0 7 * * *", "UTC"), None)));
        // The daily path takes the zone too
        let daily = Some((Schedule::Daily { time: "07:00".into(), tz: "Europe/Berlin".into() }, None));
        assert_eq!(schedule_from_text("every day at 7:00 (Europe/Berlin)"), daily);
        assert_eq!(schedule_from_text("daily 07:00 Europe/Berlin"), daily);
        // Old names become current ones, as everywhere else in kwant
        assert_eq!(schedule_from_text("0 7 * * * (Europe/Kiev)").map(|(s, _)| s), Some(cron("0 7 * * *", "Europe/Kyiv")));
    }

    #[test]
    fn unknown_schedule_zones_fall_back_to_utc_with_a_note() {
        for text in ["0 7 * * * (Mars/Olympus)", "0 7 * * * tz=Nowhere", "CRON_TZ=../../etc/passwd 0 7 * * *", "0 7 * * * (UTC+2)"] {
            let (s, note) = schedule_from_text(text).unwrap_or_else(|| panic!("{text}"));
            assert_eq!(s, cron("0 7 * * *", "UTC"), "{text}");
            assert!(note.as_deref().unwrap_or("").contains("so we used UTC"), "{text}: {note:?}");
        }
        let (s, note) = schedule_from_text("daily 07:00 tz=Mars/Olympus").unwrap();
        assert_eq!(s, Schedule::Daily { time: "07:00".into(), tz: "UTC".into() });
        assert!(note.unwrap().contains("Mars/Olympus"));
    }

    #[test]
    fn json_schedules_keep_their_zone_or_fall_back_to_utc() {
        let berlin = cron("0 7 * * *", "Europe/Berlin");
        let j = |v| read_schedule(&routine(Some(v), None));
        assert_eq!(j(serde_json::json!({"type": "cron", "expression": "0 7 * * *", "tz": "Europe/Berlin"})), Some((berlin.clone(), None)));
        assert_eq!(j(serde_json::json!({"type": "cron", "expression": "0 7 * * *", "timeZone": "Europe/Berlin"})), Some((berlin.clone(), None)));
        // An empty or unknown zone (OpenMausBot packages can leave timeZone out) keeps the expression in UTC.
        for tz in ["", "Mars/Olympus", "<time zone like Europe/Berlin, or UTC>"] {
            let (s, note) = j(serde_json::json!({"type": "cron", "expression": "0 7 * * *", "tz": tz})).unwrap();
            assert_eq!(s, cron("0 7 * * *", "UTC"), "{tz}");
            assert!(note.unwrap().contains("so we used UTC"), "{tz}");
        }
        // A schedule written as a string, and a broken object with usable text
        assert_eq!(j(serde_json::json!("0 7 * * * (Europe/Berlin)")), Some((berlin.clone(), None)));
        let broken = routine(Some(serde_json::json!({"type": "cron", "expression": "<cron line>", "tz": "UTC"})), Some("0 7 * * * (Europe/Berlin)"));
        assert_eq!(read_schedule(&broken), Some((berlin, None)));
        assert_eq!(read_schedule(&routine(None, Some("whenever"))), None);
    }

    #[test]
    fn slugs() {
        assert_eq!(slug("/Release Checklist!"), "release-checklist");
        assert_eq!(slug("???"), "imported-skill");
    }
}