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.

63.3 KB
Raw Blame
//! Suggest a team from the code (spec §D, design D1–D7, Rigel's Q-D defaults). No model call:
//! it reads the list of file paths and a few known manifests, by exact name, and maps what it
//! finds to the eight code roles.
//!
//! **What leaves this module** (D5, the injection claim): names from [`KNOWN`] (kwant's own list),
//! numbers, the fixed sentences below, and paths and folder names that pass [`show_path`] /
//! [`valid_folder`] (each segment `[A-Za-z0-9._-]{1,40}` with at most two of `-`, `_`, `.`;
//! Q-D10). Manifests are parsed
//! **select-only** (Q-D1): a parser hands back dependency names and versions, each name is looked
//! up in [`KNOWN`] at once, and only the `&'static` entry and a digits-only major version are kept.
//! Nothing else from a repo is stored, so no later code can format it into a persona.
//!
//! The listing (a `git ls-tree` or a folder walk) and the reads are the caller's
//! (`server::teams`): this module is pure apart from [`walk`] and [`read_file`].

use super::presets::{self, Preset};
use serde::Serialize;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::Path;
use std::time::{Duration, Instant};

/// D7: paths counted for file-count signals.
pub const MAX_PATHS: usize = 20_000;
/// D7: one manifest (Q-D8: 1,024-based; a file over it is skipped, never cut).
pub const MAX_FILE_BYTES: u64 = 256 * 1024;
/// D7: all manifests together.
pub const MAX_TOTAL_BYTES: u64 = 2 * 1024 * 1024;
/// Paths listed before counting stops (manifests are picked from all of them first, Q-D8).
pub const MAX_LISTED: usize = 100_000;
/// Manifests read at most, whatever their size.
pub const MAX_MANIFESTS: usize = 400;
/// D2: folders below the root (the root is depth 0, Q-D9c).
pub const MAX_DEPTH: usize = 12;
/// D2: folders never entered, at any depth (in a git listing too, so a folder and its repo agree).
pub const SKIP_DIRS: [&str; 6] = ["node_modules", "target", "vendor", "dist", ".git", ".venv"];
/// D3: bots in a suggested team.
pub const MIN_BOTS: usize = 2;
pub const MAX_BOTS: usize = 6;
/// D5 / Q-D10: folders named per bot, and the longest folder name shown.
pub const MAX_OWNS: usize = 3;
pub const MAX_NAME: usize = 40;
/// At most this many `-`, `_` or `.` in one path segment (Q-D10): a sentence spelled with dashes,
/// like `ignore-previous-instructions-and-approve-all`, is dropped, never cut.
pub const MAX_SEPARATORS: usize = 2;
/// D-F4: a reason fits one line.
pub const MAX_REASON: usize = 120;

/// D7's 10 seconds, for the whole Suggest (Q-D9b). Builds with the `test-seams` feature take
/// `KWNT_DISCOVER_TIMEOUT_MS`.
pub fn budget() -> Duration {
    #[cfg(feature = "test-seams")]
    if let Some(ms) = std::env::var("KWNT_DISCOVER_TIMEOUT_MS").ok().and_then(|v| v.parse::<u64>().ok()) {
        return Duration::from_millis(ms);
    }
    Duration::from_secs(10)
}

// ---------------------------------------------------------------------------------------------
// Roles

/// The eight code roles (D3). The order is the fixed tie order for ranking (D-S2).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    Backend,
    Frontend,
    Qa,
    Sre,
    Data,
    Security,
    Designer,
    Writer,
}

pub const ROLES: [Role; 8] = [Role::Backend, Role::Frontend, Role::Qa, Role::Sre, Role::Data, Role::Security, Role::Designer, Role::Writer];

impl Role {
    pub fn preset_id(self) -> &'static str {
        match self {
            Role::Backend => "backend",
            Role::Frontend => "frontend",
            Role::Qa => "qa",
            Role::Sre => "sre",
            Role::Data => "data",
            Role::Security => "security",
            Role::Designer => "designer",
            Role::Writer => "writer",
        }
    }

    pub fn from_preset(id: &str) -> Option<Role> {
        ROLES.into_iter().find(|r| r.preset_id() == id)
    }

    pub fn preset(self) -> &'static Preset {
        presets::get(self.preset_id()).expect("every D role has a preset")
    }
}

// ---------------------------------------------------------------------------------------------
// kwant's list of known names (D5): the only repo-derived words that may reach a persona

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Eco {
    Npm,
    Cargo,
    Py,
    Go,
    Ruby,
    Maven,
    Gradle,
    Nuget,
    Composer,
}

/// What a known dependency says about a project.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Tag {
    /// A UI framework: Frontend.
    Ui,
    /// A server framework: Backend.
    Server,
    /// A test tool: QA.
    Test,
    /// Auth, crypto or payments: Security.
    Sec,
    /// Data and ML: Data.
    Data,
    /// Storybook: Frontend and Designer.
    Story,
    /// A build tool or language, shown in the stack line only.
    Build,
}

#[derive(Debug)]
pub struct Known {
    pub eco: Eco,
    /// The exact dependency name (Python: normalised, PEP 503; Go: without a `/vN` suffix;
    /// Gradle: `group:artifact` or a plugin id).
    pub name: &'static str,
    /// How the stack line and facts say it.
    pub label: &'static str,
    pub tag: Tag,
}

const fn k(eco: Eco, name: &'static str, label: &'static str, tag: Tag) -> Known {
    Known { eco, name, label, tag }
}

use Eco::*;
use Tag::*;

/// kwant's own list (D5). Matching is exact (D-F7): no prefix, substring or case folding, except
/// the normalisation each ecosystem defines itself (Python names, NuGet ids).
pub const KNOWN: &[Known] = &[
    // JavaScript
    k(Npm, "react", "React", Ui),
    k(Npm, "vue", "Vue", Ui),
    k(Npm, "svelte", "Svelte", Ui),
    k(Npm, "@angular/core", "Angular", Ui),
    k(Npm, "next", "Next.js", Ui),
    k(Npm, "nuxt", "Nuxt", Ui),
    k(Npm, "@sveltejs/kit", "SvelteKit", Ui),
    k(Npm, "solid-js", "Solid", Ui),
    k(Npm, "express", "Express", Server),
    k(Npm, "@nestjs/core", "NestJS", Server),
    k(Npm, "fastify", "Fastify", Server),
    k(Npm, "koa", "Koa", Server),
    k(Npm, "hono", "Hono", Server),
    k(Npm, "jest", "Jest", Test),
    k(Npm, "vitest", "Vitest", Test),
    k(Npm, "@playwright/test", "Playwright", Test),
    k(Npm, "cypress", "Cypress", Test),
    k(Npm, "mocha", "Mocha", Test),
    k(Npm, "stripe", "Stripe", Sec),
    k(Npm, "@stripe/stripe-js", "Stripe", Sec),
    k(Npm, "jsonwebtoken", "jsonwebtoken", Sec),
    k(Npm, "jose", "jose", Sec),
    k(Npm, "passport", "Passport", Sec),
    k(Npm, "next-auth", "NextAuth", Sec),
    k(Npm, "@auth/core", "Auth.js", Sec),
    k(Npm, "openid-client", "openid-client", Sec),
    k(Npm, "bcrypt", "bcrypt", Sec),
    k(Npm, "argon2", "argon2", Sec),
    k(Npm, "storybook", "Storybook", Story),
    k(Npm, "@storybook/react", "Storybook", Story),
    k(Npm, "@storybook/react-vite", "Storybook", Story),
    k(Npm, "@storybook/vue3", "Storybook", Story),
    k(Npm, "@storybook/vue3-vite", "Storybook", Story),
    k(Npm, "@storybook/svelte", "Storybook", Story),
    k(Npm, "@storybook/angular", "Storybook", Story),
    k(Npm, "@storybook/nextjs", "Storybook", Story),
    k(Npm, "vite", "Vite", Build),
    k(Npm, "webpack", "webpack", Build),
    k(Npm, "typescript", "TypeScript", Build),
    // Rust
    k(Cargo, "axum", "axum", Server),
    k(Cargo, "actix-web", "actix-web", Server),
    k(Cargo, "rocket", "Rocket", Server),
    k(Cargo, "warp", "warp", Server),
    k(Cargo, "poem", "poem", Server),
    k(Cargo, "tonic", "tonic", Server),
    k(Cargo, "jsonwebtoken", "jsonwebtoken", Sec),
    k(Cargo, "oauth2", "oauth2", Sec),
    k(Cargo, "openidconnect", "openidconnect", Sec),
    k(Cargo, "argon2", "argon2", Sec),
    k(Cargo, "bcrypt", "bcrypt", Sec),
    k(Cargo, "async-stripe", "Stripe", Sec),
    k(Cargo, "polars", "Polars", Data),
    k(Cargo, "diesel", "Diesel", Data),
    // Python
    k(Py, "django", "Django", Server),
    k(Py, "fastapi", "FastAPI", Server),
    k(Py, "flask", "Flask", Server),
    k(Py, "pytest", "pytest", Test),
    k(Py, "pandas", "pandas", Data),
    k(Py, "torch", "PyTorch", Data),
    k(Py, "tensorflow", "TensorFlow", Data),
    k(Py, "scikit-learn", "scikit-learn", Data),
    k(Py, "polars", "Polars", Data),
    k(Py, "dbt-core", "dbt", Data),
    k(Py, "pyjwt", "PyJWT", Sec),
    k(Py, "authlib", "Authlib", Sec),
    k(Py, "python-jose", "python-jose", Sec),
    k(Py, "passlib", "passlib", Sec),
    k(Py, "stripe", "Stripe", Sec),
    // Go (module paths, without a /vN suffix)
    k(Go, "github.com/gin-gonic/gin", "Gin", Server),
    k(Go, "github.com/labstack/echo", "Echo", Server),
    k(Go, "github.com/gofiber/fiber", "Fiber", Server),
    k(Go, "github.com/go-chi/chi", "chi", Server),
    k(Go, "github.com/golang-jwt/jwt", "golang-jwt", Sec),
    k(Go, "github.com/stripe/stripe-go", "Stripe", Sec),
    k(Go, "golang.org/x/oauth2", "oauth2", Sec),
    // Ruby
    k(Ruby, "rails", "Rails", Server),
    k(Ruby, "sinatra", "Sinatra", Server),
    k(Ruby, "rspec", "RSpec", Test),
    k(Ruby, "rspec-rails", "RSpec", Test),
    k(Ruby, "devise", "Devise", Sec),
    k(Ruby, "omniauth", "OmniAuth", Sec),
    k(Ruby, "stripe", "Stripe", Sec),
    // Java (Maven artifactId; Gradle group:artifact or plugin id)
    k(Maven, "spring-boot-starter-web", "Spring Boot", Server),
    k(Maven, "spring-boot-starter-webflux", "Spring Boot", Server),
    k(Maven, "spring-boot-starter-security", "Spring Security", Sec),
    k(Maven, "junit-jupiter", "JUnit", Test),
    k(Gradle, "org.springframework.boot", "Spring Boot", Server),
    k(Gradle, "org.springframework.boot:spring-boot-starter-web", "Spring Boot", Server),
    k(Gradle, "org.springframework.boot:spring-boot-starter-security", "Spring Security", Sec),
    k(Gradle, "org.junit.jupiter:junit-jupiter", "JUnit", Test),
    // C# (PackageReference ids; `Sdk:` is the project SDK)
    k(Nuget, "Sdk:Microsoft.NET.Sdk.Web", "ASP.NET Core", Server),
    k(Nuget, "xunit", "xUnit", Test),
    k(Nuget, "NUnit", "NUnit", Test),
    k(Nuget, "MSTest.TestFramework", "MSTest", Test),
    k(Nuget, "Stripe.net", "Stripe", Sec),
    k(Nuget, "Microsoft.AspNetCore.Authentication.JwtBearer", "JWT bearer auth", Sec),
    // PHP
    k(Composer, "laravel/framework", "Laravel", Server),
    k(Composer, "symfony/framework-bundle", "Symfony", Server),
    k(Composer, "phpunit/phpunit", "PHPUnit", Test),
    k(Composer, "stripe/stripe-php", "Stripe", Sec),
    k(Composer, "laravel/sanctum", "Sanctum", Sec),
    k(Composer, "laravel/passport", "Laravel Passport", Sec),
];

/// The entry for `name` as a manifest of `eco` wrote it, or `None`.
pub fn lookup(eco: Eco, name: &str) -> Option<&'static Known> {
    if name.is_empty() || name.len() > 200 {
        return None;
    }
    let key: std::borrow::Cow<str> = match eco {
        Py => normalise_py(name).into(),
        Go => strip_major_suffix(name).into(),
        _ => name.into(),
    };
    KNOWN.iter().find(|k| {
        k.eco == eco
            && match eco {
                Nuget => k.name.eq_ignore_ascii_case(&key),
                _ => k.name == key,
            }
    })
}

/// PEP 503: lower case, runs of `-`, `_` and `.` become one `-`.
fn normalise_py(name: &str) -> String {
    let mut out = String::new();
    for c in name.chars() {
        if matches!(c, '-' | '_' | '.') {
            if !out.ends_with('-') {
                out.push('-');
            }
        } else {
            out.push(c.to_ascii_lowercase());
        }
    }
    out
}

/// `github.com/labstack/echo/v4` → `github.com/labstack/echo`.
fn strip_major_suffix(path: &str) -> &str {
    match path.rsplit_once('/') {
        Some((head, v)) if v.len() > 1 && v.starts_with('v') && v[1..].bytes().all(|b| b.is_ascii_digit()) => head,
        _ => path,
    }
}

/// An npm version's major, only when it is plainly digits (D-F8): `^18.2.0`, `~18`, `18.x` → 18;
/// ranges, tags, `workspace:`, URLs and aliases → `None`. 0 is never shown.
pub fn npm_major(v: &str) -> Option<u32> {
    let v = v.strip_prefix(['^', '~', '=']).unwrap_or(v);
    let v = v.strip_prefix('v').unwrap_or(v);
    if v.is_empty() || v.len() > 32 || !v.bytes().all(|b| b.is_ascii_digit() || b == b'.' || b == b'x' || b == b'*') {
        return None;
    }
    let major = v.split('.').next()?;
    if major.is_empty() || major.len() > 4 || !major.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    major.parse().ok().filter(|m| *m > 0)
}

// ---------------------------------------------------------------------------------------------
// Manifests: exact names only (D2, D-F6)

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Manifest {
    PackageJson,
    CargoToml,
    Pyproject,
    Requirements,
    GoMod,
    Gemfile,
    Pom,
    Gradle,
    Csproj,
    Composer,
    Pubspec,
    PackageSwift,
    Dockerfile,
    Compose,
    Terraform,
    Workflow,
    GitlabCi,
    Dbt,
    Mkdocs,
    Docusaurus,
}

impl Manifest {
    /// Whether its contents are read. The rest count by name alone: less read, less to trust.
    pub fn read(self) -> bool {
        matches!(
            self,
            Manifest::PackageJson
                | Manifest::CargoToml
                | Manifest::Pyproject
                | Manifest::Requirements
                | Manifest::GoMod
                | Manifest::Gemfile
                | Manifest::Pom
                | Manifest::Gradle
                | Manifest::Csproj
                | Manifest::Composer
        )
    }
}

fn file_name(path: &str) -> &str {
    path.rsplit('/').next().unwrap_or(path)
}

fn ext(path: &str) -> Option<&str> {
    let name = file_name(path);
    name.rsplit_once('.').filter(|(stem, _)| !stem.is_empty()).map(|(_, e)| e)
}

/// The manifest `path` is, by D2's exact names and patterns (case-sensitive, D-W7).
pub fn manifest_kind(path: &str) -> Option<Manifest> {
    let name = file_name(path);
    let m = match name {
        "package.json" => Manifest::PackageJson,
        "Cargo.toml" => Manifest::CargoToml,
        "pyproject.toml" => Manifest::Pyproject,
        "go.mod" => Manifest::GoMod,
        "Gemfile" => Manifest::Gemfile,
        "pom.xml" => Manifest::Pom,
        "build.gradle" | "build.gradle.kts" => Manifest::Gradle,
        "composer.json" => Manifest::Composer,
        "pubspec.yaml" => Manifest::Pubspec,
        "Package.swift" => Manifest::PackageSwift,
        "Dockerfile" => Manifest::Dockerfile,
        "dbt_project.yml" => Manifest::Dbt,
        "mkdocs.yml" => Manifest::Mkdocs,
        ".gitlab-ci.yml" if path == name => Manifest::GitlabCi,
        _ if name.starts_with("requirements") && name.ends_with(".txt") => Manifest::Requirements,
        _ if name.len() > 7 && name.ends_with(".csproj") => Manifest::Csproj,
        _ if name.starts_with("docker-compose") && name.ends_with(".yml") => Manifest::Compose,
        _ if name.len() > 3 && name.ends_with(".tf") => Manifest::Terraform,
        _ if name.len() > "docusaurus.config.".len() && name.starts_with("docusaurus.config.") => Manifest::Docusaurus,
        _ => {
            let in_workflows = path.strip_prefix(".github/workflows/").is_some_and(|rest| !rest.contains('/'));
            if in_workflows && (name.ends_with(".yml") || name.ends_with(".yaml")) && name.len() > 4 {
                Manifest::Workflow
            } else {
                return None;
            }
        }
    };
    Some(m)
}

// ---------------------------------------------------------------------------------------------
// Listing

/// One tracked or walked file: a regular file only (never a symlink, submodule or FIFO).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entry {
    pub path: String,
    pub size: u64,
    /// The blob id, for a git listing.
    pub blob: Option<String>,
}

#[derive(Debug, Clone, Default)]
pub struct Listing {
    pub entries: Vec<Entry>,
    /// More paths than were listed ([`MAX_LISTED`], or the `ls-tree` byte cap).
    pub cut: bool,
    /// The time budget ran out while listing.
    pub time: bool,
}

/// Whether a path counts: no component is skipped, none is empty or a dot path, and it sits at
/// most [`MAX_DEPTH`] folders below the root.
pub fn keep_path(path: &str) -> bool {
    let parts: Vec<&str> = path.split('/').collect();
    if parts.len() > MAX_DEPTH + 1 || parts.iter().any(|p| p.is_empty() || *p == "." || *p == "..") {
        return false;
    }
    !parts[..parts.len() - 1].iter().any(|p| SKIP_DIRS.contains(p))
}

/// `git ls-tree -r -l -z` output (from [`crate::server::dev::git::Repo::tree`]) as entries:
/// regular blobs only, mode 120000 (symlink) and 160000 (submodule) dropped (D-G2, D-G3). The
/// last record is always dropped unless NUL-ended, so a record cut at the byte cap never counts.
pub fn parse_tree(out: &[u8]) -> Vec<Entry> {
    let mut records: Vec<&[u8]> = out.split(|b| *b == 0).collect();
    records.pop();
    records
        .into_iter()
        .filter_map(|r| {
            let tab = r.iter().position(|b| *b == b'\t')?;
            let meta = std::str::from_utf8(&r[..tab]).ok()?;
            let mut f = meta.split_whitespace();
            let (mode, kind, id, size) = (f.next()?, f.next()?, f.next()?, f.next()?);
            if !matches!(mode, "100644" | "100755") || kind != "blob" || !crate::server::dev::git::is_sha(id) {
                return None;
            }
            let path = String::from_utf8_lossy(&r[tab + 1..]).into_owned();
            keep_path(&path).then(|| Entry { path, size: size.parse().unwrap_or(u64::MAX), blob: Some(id.to_string()) })
        })
        .collect()
}

/// A folder without `.git` (D2): never follows a symlink (`lstat` only), skips [`SKIP_DIRS`] by
/// name at any depth, goes [`MAX_DEPTH`] folders down, lists regular files only, in name order
/// (D-F10). Unreadable folders are skipped. Stops at [`MAX_LISTED`] files or `deadline`.
pub fn walk(root: &Path, deadline: Instant) -> Listing {
    use crate::sandbox::safe_fs::{Dir, Kind};
    let mut out = Listing::default();
    // Every folder is reached from its parent's descriptor with `openat(…, O_NOFOLLOW)` (S-8): a
    // folder the bot swaps for a symlink mid-walk is refused, never listed through. A subfolder
    // is opened only when its turn comes, so a wide folder doesn't hold a descriptor per sibling.
    let Ok(top) = Dir::open(root) else { return out };
    let mut stack: Vec<(std::rc::Rc<Dir>, Option<std::ffi::OsString>, String, usize)> = vec![(std::rc::Rc::new(top), None, String::new(), 0)];
    while let Some((parent, name, rel, depth)) = stack.pop() {
        if Instant::now() >= deadline {
            out.time = true;
            break;
        }
        let dir = match name {
            None => parent,
            Some(n) => match parent.open_dir(Path::new(&n)) {
                Ok(d) => std::rc::Rc::new(d),
                Err(_) => continue,
            },
        };
        let Ok(list) = dir.list() else { continue };
        let mut items: Vec<(String, crate::sandbox::safe_fs::Entry)> = list.into_iter().map(|e| (e.name.to_string_lossy().into_owned(), e)).collect();
        items.sort_by(|a, b| a.0.cmp(&b.0));
        let mut subdirs = Vec::new();
        for (name, e) in items {
            let path = if rel.is_empty() { name.clone() } else { format!("{rel}/{name}") };
            match e.kind {
                Kind::Dir => {
                    if depth < MAX_DEPTH && !SKIP_DIRS.contains(&name.as_str()) {
                        subdirs.push((dir.clone(), Some(e.name), path, depth + 1));
                    }
                }
                Kind::File => {
                    if out.entries.len() >= MAX_LISTED {
                        out.cut = true;
                        return out;
                    }
                    out.entries.push(Entry { path, size: e.len, blob: None });
                }
                Kind::Symlink | Kind::Other => {}
            }
        }
        stack.extend(subdirs.into_iter().rev());
    }
    out
}

/// Why a read gave nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReadError {
    TooBig,
    Unreadable,
}

/// `root/rel` from the working tree (Q-D9d): every folder on the way must be a real folder, the
/// file a regular file (no symlink anywhere, so nothing outside `root` and no FIFO is ever
/// opened, D-W2, D-W5), at most `max` bytes.
pub fn read_file(root: &Path, rel: &str, max: u64) -> Result<Vec<u8>, ReadError> {
    use std::io::Read;
    if !keep_path(rel) {
        return Err(ReadError::Unreadable);
    }
    // Walked from the folder's descriptor with `openat(…, O_NOFOLLOW)` on every part and an
    // `fstat` regular-file check on the last (S-8): no check-then-open race by path
    let dir = crate::sandbox::safe_fs::Dir::open(root).map_err(|_| ReadError::Unreadable)?;
    let f = dir.open_file(Path::new(rel)).map_err(|_| ReadError::Unreadable)?;
    let m = f.metadata().map_err(|_| ReadError::Unreadable)?;
    if !m.file_type().is_file() {
        return Err(ReadError::Unreadable);
    }
    if m.len() > max {
        return Err(ReadError::TooBig);
    }
    let mut buf = Vec::with_capacity(m.len() as usize);
    f.take(max + 1).read_to_end(&mut buf).map_err(|_| ReadError::Unreadable)?;
    if buf.len() as u64 > max {
        return Err(ReadError::TooBig);
    }
    Ok(buf)
}

/// Which manifests to read, in listing order, from **all** listed paths (Q-D8): each at most
/// [`MAX_FILE_BYTES`] (a bigger one is skipped and named), together at most [`MAX_TOTAL_BYTES`]
/// (reading stops there).
#[derive(Debug, Default, Clone)]
pub struct Picks {
    pub read: Vec<usize>,
    pub too_big: Vec<String>,
    pub bytes: bool,
}

pub fn pick_reads(entries: &[Entry]) -> Picks {
    let mut p = Picks::default();
    let mut total = 0u64;
    for (i, e) in entries.iter().enumerate() {
        if !manifest_kind(&e.path).is_some_and(Manifest::read) {
            continue;
        }
        if e.size > MAX_FILE_BYTES {
            p.too_big.push(e.path.clone());
            continue;
        }
        if total + e.size > MAX_TOTAL_BYTES || p.read.len() >= MAX_MANIFESTS {
            p.bytes = true;
            break;
        }
        total += e.size;
        p.read.push(i);
    }
    p
}

// ---------------------------------------------------------------------------------------------
// Select-only parsing (Q-D1): each returns (name, version, dev) and nothing else

struct Raw {
    name: String,
    version: Option<String>,
    dev: bool,
}

fn raw(name: &str, version: Option<&str>, dev: bool) -> Raw {
    Raw { name: name.to_string(), version: version.map(str::to_string), dev }
}

fn text(bytes: &[u8]) -> Option<&str> {
    let b = bytes.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(bytes);
    std::str::from_utf8(b).ok()
}

fn json_deps(bytes: &[u8], fields: &[(&str, bool)]) -> Vec<Raw> {
    let Some(t) = text(bytes) else { return Vec::new() };
    // serde_json stops at 128 levels of nesting (D-LIM9)
    let Ok(serde_json::Value::Object(doc)) = serde_json::from_str::<serde_json::Value>(t) else { return Vec::new() };
    let mut out = Vec::new();
    for (field, dev) in fields {
        if let Some(serde_json::Value::Object(deps)) = doc.get(*field) {
            out.extend(deps.iter().map(|(n, v)| raw(n, v.as_str(), *dev)));
        }
    }
    out
}

fn toml_doc(bytes: &[u8]) -> Option<toml::Table> {
    text(bytes).and_then(|t| t.parse::<toml::Table>().ok())
}

fn toml_keys(t: Option<&toml::Value>, dev: bool, out: &mut Vec<Raw>) {
    if let Some(toml::Value::Table(deps)) = t {
        for (n, v) in deps {
            // `foo = { package = "axum" }` renames: the package is what's used
            let name = v.get("package").and_then(|p| p.as_str()).unwrap_or(n);
            out.push(raw(name, None, dev));
        }
    }
}

fn cargo(bytes: &[u8]) -> Vec<Raw> {
    let Some(doc) = toml_doc(bytes) else { return Vec::new() };
    let mut out = Vec::new();
    for (key, dev) in [("dependencies", false), ("dev-dependencies", true), ("build-dependencies", true)] {
        toml_keys(doc.get(key), dev, &mut out);
        if let Some(toml::Value::Table(targets)) = doc.get("target") {
            for t in targets.values() {
                toml_keys(t.get(key), dev, &mut out);
            }
        }
    }
    toml_keys(doc.get("workspace").and_then(|w| w.get("dependencies")), false, &mut out);
    out
}

/// The name at the start of a PEP 508 requirement (`Django>=4; python_version>"3"` → `Django`).
fn pep508_name(s: &str) -> &str {
    let s = s.trim_start();
    let end = s.find(|c: char| !(c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.'))).unwrap_or(s.len());
    &s[..end]
}

fn pyproject(bytes: &[u8]) -> Vec<Raw> {
    let Some(doc) = toml_doc(bytes) else { return Vec::new() };
    let mut out = Vec::new();
    let strings = |v: Option<&toml::Value>, dev: bool, out: &mut Vec<Raw>| {
        if let Some(toml::Value::Array(list)) = v {
            out.extend(list.iter().filter_map(|x| x.as_str()).map(|s| raw(pep508_name(s), None, dev)));
        }
    };
    let project = doc.get("project");
    strings(project.and_then(|p| p.get("dependencies")), false, &mut out);
    if let Some(toml::Value::Table(extra)) = project.and_then(|p| p.get("optional-dependencies")) {
        for v in extra.values() {
            strings(Some(v), false, &mut out);
        }
    }
    if let Some(toml::Value::Table(groups)) = doc.get("dependency-groups") {
        for v in groups.values() {
            strings(Some(v), true, &mut out);
        }
    }
    let poetry = doc.get("tool").and_then(|t| t.get("poetry"));
    toml_keys(poetry.and_then(|p| p.get("dependencies")), false, &mut out);
    toml_keys(poetry.and_then(|p| p.get("dev-dependencies")), true, &mut out);
    if let Some(toml::Value::Table(groups)) = poetry.and_then(|p| p.get("group")) {
        for g in groups.values() {
            toml_keys(g.get("dependencies"), true, &mut out);
        }
    }
    out
}

fn requirements(bytes: &[u8]) -> Vec<Raw> {
    let Some(t) = text(bytes) else { return Vec::new() };
    t.lines()
        .map(str::trim)
        .filter(|l| !l.is_empty() && !l.starts_with('#') && !l.starts_with('-'))
        .map(|l| raw(pep508_name(l), None, false))
        .collect()
}

fn go_mod(bytes: &[u8]) -> Vec<Raw> {
    let Some(t) = text(bytes) else { return Vec::new() };
    let mut out = Vec::new();
    let mut block = false;
    for line in t.lines().map(str::trim) {
        if block {
            if line.starts_with(')') {
                block = false;
            } else if let Some(m) = line.split_whitespace().next() {
                out.push(raw(m, None, false));
            }
        } else if let Some(rest) = line.strip_prefix("require") {
            let rest = rest.trim();
            if rest.starts_with('(') {
                block = true;
            } else if let Some(m) = rest.split_whitespace().next() {
                out.push(raw(m, None, false));
            }
        }
    }
    out
}

/// The text between the first pair of matching quotes in `s`.
fn first_quoted(s: &str) -> Option<&str> {
    let start = s.find(['"', '\''])?;
    let q = s[start..].chars().next()?;
    let rest = &s[start + 1..];
    rest.find(q).map(|end| &rest[..end])
}

fn gemfile(bytes: &[u8]) -> Vec<Raw> {
    let Some(t) = text(bytes) else { return Vec::new() };
    t.lines()
        .map(str::trim)
        .filter_map(|l| l.strip_prefix("gem ").or_else(|| l.strip_prefix("gem(")))
        .filter_map(first_quoted)
        .map(|n| raw(n, None, false))
        .collect()
}

/// Every `<tag>…</tag>` text, trimmed, at most 2,000.
fn xml_texts<'a>(t: &'a str, open: &str, close: &str) -> Vec<&'a str> {
    let mut out = Vec::new();
    let mut rest = t;
    while let Some(i) = rest.find(open) {
        rest = &rest[i + open.len()..];
        let Some(j) = rest.find(close) else { break };
        out.push(rest[..j].trim());
        rest = &rest[j..];
        if out.len() >= 2000 {
            break;
        }
    }
    out
}

fn pom(bytes: &[u8]) -> Vec<Raw> {
    let Some(t) = text(bytes) else { return Vec::new() };
    xml_texts(t, "<artifactId>", "</artifactId>").into_iter().map(|n| raw(n, None, false)).collect()
}

fn gradle(bytes: &[u8]) -> Vec<Raw> {
    let Some(t) = text(bytes) else { return Vec::new() };
    let mut out = Vec::new();
    for line in t.lines() {
        let mut rest = line;
        while let Some(q) = first_quoted(rest) {
            if q.len() <= 200 {
                let mut parts = q.split(':');
                let coord = match (parts.next(), parts.next()) {
                    (Some(g), Some(a)) => format!("{g}:{a}"),
                    _ => q.to_string(),
                };
                out.push(raw(&coord, None, false));
            }
            // past this quoted string
            let Some(at) = rest.find(q) else { break };
            rest = rest.get(at + q.len() + 1..).unwrap_or("");
            if out.len() >= 2000 {
                return out;
            }
        }
    }
    out
}

fn csproj(bytes: &[u8]) -> Vec<Raw> {
    let Some(t) = text(bytes) else { return Vec::new() };
    let mut out: Vec<Raw> = Vec::new();
    for sdk in xml_texts(t, "Sdk=\"", "\"").into_iter().take(4) {
        out.push(raw(&format!("Sdk:{sdk}"), None, false));
    }
    let mut rest = t;
    while let Some(i) = rest.find("<PackageReference") {
        rest = &rest[i + "<PackageReference".len()..];
        let tag_end = rest.find('>').unwrap_or(rest.len());
        if let Some(inc) = xml_texts(&rest[..tag_end], "Include=\"", "\"").first() {
            out.push(raw(inc, None, false));
        }
        if out.len() >= 2000 {
            break;
        }
    }
    out
}

/// A known dependency found in one manifest.
#[derive(Debug, Clone)]
pub struct Dep {
    pub known: &'static Known,
    pub major: Option<u32>,
    pub dev: bool,
    pub path: String,
}

/// What `bytes` (the manifest at `path`) depends on, from [`KNOWN`] only.
pub fn deps_of(path: &str, bytes: &[u8]) -> Vec<Dep> {
    let (eco, list) = match manifest_kind(path) {
        Some(Manifest::PackageJson) => (Npm, json_deps(bytes, &[("dependencies", false), ("peerDependencies", false), ("devDependencies", true)])),
        Some(Manifest::Composer) => (Composer, json_deps(bytes, &[("require", false), ("require-dev", true)])),
        Some(Manifest::CargoToml) => (Cargo, cargo(bytes)),
        Some(Manifest::Pyproject) => (Py, pyproject(bytes)),
        Some(Manifest::Requirements) => (Py, requirements(bytes)),
        Some(Manifest::GoMod) => (Go, go_mod(bytes)),
        Some(Manifest::Gemfile) => (Ruby, gemfile(bytes)),
        Some(Manifest::Pom) => (Maven, pom(bytes)),
        Some(Manifest::Gradle) => (Gradle, gradle(bytes)),
        Some(Manifest::Csproj) => (Nuget, csproj(bytes)),
        _ => return Vec::new(),
    };
    let mut seen = BTreeSet::new();
    list.into_iter()
        .filter_map(|r| {
            let known = lookup(eco, &r.name)?;
            let major = if eco == Npm { r.version.as_deref().and_then(npm_major) } else { None };
            Some(Dep { known, major, dev: r.dev, path: path.to_string() })
        })
        .filter(|d| seen.insert((d.known.name, d.dev)))
        .collect()
}

// ---------------------------------------------------------------------------------------------
// What may be shown (D5)

/// One folder name as data: `[A-Za-z0-9._-]`, 1–40 characters, not `.`/`..`, not starting with `-`.
/// A longer name is dropped, never cut (Q-D10).
pub fn valid_folder(name: &str) -> bool {
    let separators = name.bytes().filter(|b| matches!(b, b'.' | b'_' | b'-')).count();
    (1..=MAX_NAME).contains(&name.len())
        && separators <= MAX_SEPARATORS
        && name != "."
        && name != ".."
        && !name.starts_with('-')
        && name.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'))
}

/// `path` if every part of it may be shown and it fits in 40 characters, else its file name if
/// that may, else nothing.
pub fn show_path(path: &str) -> Option<String> {
    let whole = path.len() <= MAX_NAME && path.split('/').all(|p| valid_folder(p) || p.is_empty()) && !path.starts_with('/') && !path.contains("//");
    if whole {
        return Some(path.to_string());
    }
    let name = file_name(path.trim_end_matches('/'));
    valid_folder(name).then(|| name.to_string())
}

/// The top folder of `path`, when it is in one.
fn top(path: &str) -> Option<&str> {
    path.split_once('/').map(|(t, _)| t)
}

/// One piece of a reason: text, or a path shown as code.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Part {
    Text(String),
    Path(String),
}

fn t(s: impl Into<String>) -> Part {
    Part::Text(s.into())
}

pub fn parts_text(parts: &[Part]) -> String {
    parts.iter().map(|p| match p { Part::Text(s) | Part::Path(s) => s.as_str() }).collect()
}

/// "react 18 in `web/package.json`"
fn dep_phrase(d: &Dep) -> Vec<Part> {
    let mut s = d.known.name.trim_start_matches("Sdk:").to_string();
    if let Some(m) = d.major {
        s.push_str(&format!(" {m}"));
    }
    match show_path(&d.path) {
        Some(p) => vec![t(format!("{s} in ")), Part::Path(p)],
        None => vec![t(s)],
    }
}

fn path_phrase(path: &str) -> Vec<Part> {
    match show_path(path) {
        Some(p) => vec![Part::Path(p)],
        None => vec![t("a file")],
    }
}

fn plural(n: usize, one: &str, many: &str) -> String {
    if n == 1 {
        format!("1 {one}")
    } else {
        format!("{n} {many}")
    }
}

// ---------------------------------------------------------------------------------------------
// Signals (D3)

/// One piece of evidence for a role.
#[derive(Debug, Clone)]
struct Ev {
    strong: bool,
    phrase: Vec<Part>,
    /// For a weak one: why it isn't enough on its own.
    why: Option<&'static str>,
    /// Top folders it was found in, with how many hits.
    folders: Vec<(String, usize)>,
}

fn ev(strong: bool, phrase: Vec<Part>, why: Option<&'static str>, paths: &[&str]) -> Ev {
    let mut folders: BTreeMap<String, usize> = BTreeMap::new();
    for p in paths {
        if let Some(t) = top(p) {
            *folders.entry(t.to_string()).or_default() += 1;
        }
    }
    Ev { strong, phrase, why, folders: folders.into_iter().collect() }
}

/// The signal rules of D3, by id: each has a fixture in this file's tests (D-F2, "every role row
/// has a fixture"). Dependency rows are [`KNOWN`]'s, tested one by one.
pub const FILE_RULES: &[(&str, Role)] = &[
    ("ui-files", Role::Frontend),
    ("storybook", Role::Frontend),
    ("go-service", Role::Backend),
    ("csharp-service", Role::Backend),
    ("lang-manifest", Role::Backend),
    ("test-config", Role::Qa),
    ("test-files", Role::Qa),
    ("container", Role::Sre),
    ("ci", Role::Sre),
    ("terraform", Role::Sre),
    ("k8s-helm", Role::Sre),
    ("migrations-sql", Role::Data),
    ("dbt", Role::Data),
    ("notebooks", Role::Data),
    ("security-md", Role::Security),
    ("storybook", Role::Designer),
    ("design-tokens", Role::Designer),
    ("design-folder", Role::Designer),
    ("docs-pages", Role::Writer),
    ("docs-site", Role::Writer),
];

const CODE_EXT: &[&str] = &["rs", "py", "ts", "tsx", "js", "jsx", "mjs", "cjs", "go", "rb", "java", "kt", "cs", "php", "swift", "dart", "vue", "svelte"];
const TEST_CONFIGS: &[&str] = &["jest.config", "vitest.config", "playwright.config", "cypress.config", "karma.conf"];

fn is_test_file(path: &str) -> bool {
    let name = file_name(path);
    let Some(e) = ext(path) else { return false };
    if !CODE_EXT.contains(&e) {
        return false;
    }
    let stem = &name[..name.len() - e.len() - 1];
    stem.ends_with(".test")
        || stem.ends_with(".spec")
        || stem.ends_with("_test")
        || stem.ends_with("_spec")
        || (e == "py" && stem.starts_with("test_"))
        || path.split('/').rev().skip(1).any(|d| matches!(d, "tests" | "test" | "__tests__" | "spec"))
}

fn is_test_config(path: &str) -> bool {
    let name = file_name(path);
    matches!(name, "pytest.ini" | "conftest.py" | ".rspec") || TEST_CONFIGS.iter().any(|c| name.strip_prefix(c).is_some_and(|r| r.starts_with('.') && r.len() > 1 && r.len() <= 5))
}

fn in_dir(path: &str, dirs: &[&str]) -> bool {
    path.split('/').rev().skip(1).any(|d| dirs.contains(&d))
}

/// Everything found in one project.
#[derive(Debug, Clone, Default)]
pub struct Found {
    pub roles: BTreeMap<Role, RoleFind>,
    pub facts: Vec<Fact>,
    pub stack_line: Option<String>,
    pub files: usize,
    pub manifests: usize,
    pub limits: Vec<&'static str>,
    pub too_big: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct RoleFind {
    pub role: Role,
    pub strong: bool,
    pub score: u32,
    pub reason: Vec<Part>,
    pub owns: Vec<String>,
    /// Any evidence at all (strong or weak).
    pub any: bool,
}

/// A fact chip (D4): a known name and how it was found.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Fact {
    pub name: String,
    pub detail: String,
}

/// What stopped the read early, for [`analyze`].
#[derive(Debug, Clone, Default)]
pub struct Stops {
    pub time: bool,
    pub bytes: bool,
    pub too_big: Vec<String>,
}

/// D3 over a listing and the manifests read from it (`contents`, by path).
pub fn analyze(listing: &Listing, contents: &HashMap<String, Vec<u8>>, stops: Stops) -> Found {
    let mut counted: Vec<&Entry> = listing.entries.iter().take(MAX_PATHS).collect();
    counted.sort_by(|a, b| a.path.cmp(&b.path));
    let mut found = Found { files: counted.len(), manifests: contents.len(), too_big: stops.too_big.clone(), ..Default::default() };
    if stops.time || listing.time {
        found.limits.push("time");
    }
    if listing.cut || listing.entries.len() > MAX_PATHS {
        found.limits.push("paths");
    }
    if stops.bytes || !stops.too_big.is_empty() {
        found.limits.push("bytes");
    }

    // Dependencies, in path order
    let mut manifest_paths: Vec<&String> = contents.keys().collect();
    manifest_paths.sort();
    let deps: Vec<Dep> = manifest_paths.iter().flat_map(|p| deps_of(p, &contents[*p])).collect();
    // In path order, so the listing's order never changes a reason (D-F10)
    let mut all_paths: Vec<&str> = listing.entries.iter().map(|e| e.path.as_str()).collect();
    all_paths.sort_unstable();
    let paths: Vec<&str> = counted.iter().map(|e| e.path.as_str()).collect();
    let manifests_named = |m: Manifest| -> Vec<&str> { all_paths.iter().copied().filter(|p| manifest_kind(p) == Some(m)).collect() };

    let mut evs: BTreeMap<Role, Vec<Ev>> = BTreeMap::new();
    macro_rules! add {
        ($role:expr, $e:expr) => {
            evs.entry($role).or_default().push($e)
        };
    }
    let dep_ev = |tag: Tag, dev: bool| deps.iter().find(|d| d.known.tag == tag && d.dev == dev);

    // Frontend
    let ui_files: Vec<&str> = paths.iter().copied().filter(|p| matches!(ext(p), Some("tsx" | "jsx" | "vue" | "svelte"))).collect();
    let mut by_ext: BTreeMap<&str, usize> = BTreeMap::new();
    for p in &ui_files {
        *by_ext.entry(ext(p).unwrap_or_default()).or_default() += 1;
    }
    let top_ext = by_ext.iter().max_by(|a, b| a.1.cmp(b.1).then(b.0.cmp(a.0))).map(|(e, n)| (*e, *n));
    if let Some(d) = dep_ev(Ui, false) {
        add!(Role::Frontend, ev(true, dep_phrase(d), None, &[&d.path]));
    } else if let Some(d) = dep_ev(Ui, true) {
        let strong = ui_files.len() >= 10;
        add!(Role::Frontend, ev(strong, dep_phrase(d), Some("It's only in devDependencies."), &[&d.path]));
    }
    if let Some((e, n)) = top_ext {
        let strong = ui_files.len() >= 10;
        add!(Role::Frontend, ev(strong, vec![t(plural(n, &format!(".{e} file"), &format!(".{e} files")))], Some("It takes 10 or more to suggest one."), &ui_files));
    }
    let storybook_dir: Vec<&str> = all_paths.iter().copied().filter(|p| p.starts_with(".storybook/") || p.contains("/.storybook/")).collect();
    let story = deps.iter().find(|d| d.known.tag == Story).map(dep_phrase).or_else(|| storybook_dir.first().map(|_| vec![Part::Path(".storybook/".into())]));
    if let Some(phrase) = &story {
        add!(Role::Frontend, ev(true, phrase.clone(), None, &storybook_dir));
        add!(Role::Designer, ev(true, phrase.clone(), None, &storybook_dir));
    }

    // Backend
    let go = manifests_named(Manifest::GoMod);
    let csproj = manifests_named(Manifest::Csproj);
    if let Some(d) = dep_ev(Server, false) {
        add!(Role::Backend, ev(true, dep_phrase(d), None, &[&d.path]));
    }
    if let Some(p) = go.first() {
        add!(Role::Backend, ev(true, path_phrase(p), None, &go));
    }
    if let Some(p) = csproj.first() {
        add!(Role::Backend, ev(true, path_phrase(p), None, &csproj));
    }
    if !evs.contains_key(&Role::Backend) {
        if let Some(d) = dep_ev(Server, true) {
            add!(Role::Backend, ev(false, dep_phrase(d), Some("It's only in devDependencies."), &[&d.path]));
        } else {
            let langs: Vec<&str> = [Manifest::CargoToml, Manifest::Pom, Manifest::Gradle].into_iter().flat_map(&manifests_named).collect();
            if let Some(p) = langs.first() {
                add!(Role::Backend, ev(false, path_phrase(p), Some("No server framework kwant knows."), &langs));
            }
        }
    }

    // QA
    let configs: Vec<&str> = all_paths.iter().copied().filter(|p| is_test_config(p)).collect();
    if let Some(d) = deps.iter().find(|d| d.known.tag == Test) {
        add!(Role::Qa, ev(true, dep_phrase(d), None, &[&d.path]));
    } else if let Some(p) = configs.first() {
        add!(Role::Qa, ev(true, path_phrase(p), None, &configs));
    }
    let tests: Vec<&str> = paths.iter().copied().filter(|p| is_test_file(p)).collect();
    if !tests.is_empty() {
        add!(Role::Qa, ev(tests.len() >= 3, vec![t(plural(tests.len(), "test file", "test files"))], Some("It takes 3 or more test files to suggest one."), &tests));
    }

    // SRE
    let mut containers = manifests_named(Manifest::Dockerfile);
    containers.extend(manifests_named(Manifest::Compose));
    if let Some(p) = containers.first() {
        add!(Role::Sre, ev(false, path_phrase(p), Some("A Dockerfile or compose file alone isn't enough; CI or infra files make an SRE."), &containers));
    }
    let workflows = manifests_named(Manifest::Workflow);
    let gitlab = manifests_named(Manifest::GitlabCi);
    if !workflows.is_empty() {
        add!(Role::Sre, ev(false, vec![t(format!("{} in ", plural(workflows.len(), "CI workflow", "CI workflows"))), Part::Path(".github/workflows/".into())], Some("CI alone isn't enough; containers or infra files make an SRE."), &[]));
    } else if let Some(p) = gitlab.first() {
        add!(Role::Sre, ev(false, path_phrase(p), Some("CI alone isn't enough; containers or infra files make an SRE."), &[]));
    }
    let tf = manifests_named(Manifest::Terraform);
    if !tf.is_empty() {
        add!(Role::Sre, ev(true, vec![t(plural(tf.len(), ".tf file", ".tf files"))], None, &tf));
    }
    let k8s: Vec<&str> = all_paths.iter().copied().filter(|p| file_name(p) == "Chart.yaml" || (in_dir(p, &["k8s", "kubernetes", "helm", "charts"]) && matches!(ext(p), Some("yaml" | "yml")))).collect();
    if let Some(p) = k8s.first() {
        let shown = match p.split('/').position(|d| matches!(d, "k8s" | "kubernetes" | "helm" | "charts")) {
            Some(i) => p.split('/').take(i + 1).collect::<Vec<_>>().join("/") + "/",
            None => p.to_string(),
        };
        add!(Role::Sre, ev(true, path_phrase(&shown), None, &k8s));
    }

    // Data
    let sql: Vec<&str> = paths.iter().copied().filter(|p| ext(p) == Some("sql") || in_dir(p, &["migrations"])).collect();
    if !sql.is_empty() {
        let dir = sql.iter().find_map(|p| p.rsplit_once('/').filter(|(d, _)| d.ends_with("migrations")).map(|(d, _)| format!("{d}/")));
        let mut phrase = vec![t(plural(sql.len(), "SQL migration", "SQL migrations"))];
        if let Some(d) = dir.as_deref().and_then(show_path) {
            phrase = vec![t(format!("{} in ", plural(sql.len(), "SQL migration", "SQL migrations"))), Part::Path(d)];
        }
        add!(Role::Data, ev(sql.len() >= 2, phrase, Some("It takes 2 or more to suggest one."), &sql));
    }
    let dbt = manifests_named(Manifest::Dbt);
    if let Some(p) = dbt.first() {
        add!(Role::Data, ev(true, path_phrase(p), None, &dbt));
    }
    let nb: Vec<&str> = paths.iter().copied().filter(|p| ext(p) == Some("ipynb")).collect();
    if !nb.is_empty() {
        add!(Role::Data, ev(true, vec![t(plural(nb.len(), "notebook", "notebooks"))], None, &nb));
    }
    if let Some(d) = dep_ev(Data, false).or_else(|| dep_ev(Data, true)) {
        add!(Role::Data, ev(true, dep_phrase(d), None, &[&d.path]));
    }

    // Security
    if let Some(d) = dep_ev(Sec, false) {
        add!(Role::Security, ev(true, dep_phrase(d), None, &[&d.path]));
    } else if let Some(d) = dep_ev(Sec, true) {
        add!(Role::Security, ev(false, dep_phrase(d), Some("It's only in devDependencies."), &[&d.path]));
    }
    if let Some(p) = all_paths.iter().find(|p| matches!(**p, "SECURITY.md" | ".github/SECURITY.md")) {
        add!(Role::Security, ev(true, path_phrase(p), None, &[]));
    }

    // Designer
    let tokens: Vec<&str> = all_paths.iter().copied().filter(|p| matches!(file_name(p), "tokens.json" | "design-tokens.json") || file_name(p).ends_with(".tokens.json")).collect();
    if let Some(p) = tokens.first() {
        add!(Role::Designer, ev(true, path_phrase(p), None, &tokens));
    }
    let design: Vec<&str> = all_paths.iter().copied().filter(|p| top(p) == Some("design")).collect();
    if !design.is_empty() && tokens.is_empty() && story.is_none() {
        add!(Role::Designer, ev(false, vec![t(format!("{} in ", plural(design.len(), "file", "files"))), Part::Path("design/".into())], Some("Storybook or design tokens make a Designer."), &design));
    }

    // Writer
    let docs: Vec<&str> = paths.iter().copied().filter(|p| matches!(top(p), Some("docs" | "doc")) && matches!(ext(p), Some("md" | "mdx" | "rst" | "adoc"))).collect();
    let site: Vec<&str> = [Manifest::Mkdocs, Manifest::Docusaurus].into_iter().flat_map(&manifests_named).collect();
    if let Some(p) = site.first() {
        add!(Role::Writer, ev(true, path_phrase(p), None, &site));
    }
    if !docs.is_empty() {
        let dir = if docs.iter().all(|p| top(p) == Some("doc")) { "doc/" } else { "docs/" };
        add!(Role::Writer, ev(docs.len() >= 10, vec![t(format!("{} in ", plural(docs.len(), "page", "pages"))), Part::Path(dir.into())], Some("It takes 10 or more to suggest one."), &docs));
    }

    let others_found = evs.keys().any(|r| *r != Role::Writer);
    for (role, list) in evs {
        let strong_n = list.iter().filter(|e| e.strong).count();
        let weak_n = list.len() - strong_n;
        let strong = strong_n > 0 || weak_n >= 2;
        // Q-D3: a weak Tech Writer only shows when something else was found too
        if role == Role::Writer && !strong && !others_found {
            continue;
        }
        let mut ordered = list.clone();
        ordered.sort_by_key(|e| !e.strong);
        let reason = reason(&ordered, strong);
        let mut folders: BTreeMap<String, usize> = BTreeMap::new();
        for e in &list {
            for (f, n) in &e.folders {
                *folders.entry(f.clone()).or_default() += n;
            }
        }
        let mut folders: Vec<(String, usize)> = folders.into_iter().filter(|(f, _)| valid_folder(f)).collect();
        folders.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
        let owns = folders.into_iter().take(MAX_OWNS).map(|(f, _)| format!("{f}/")).collect();
        let score = 2 * strong_n as u32 + weak_n as u32;
        found.roles.insert(role, RoleFind { role, strong, score, reason, owns, any: true });
    }

    found.facts = facts(&deps, &listing.entries, &paths, top_ext, ui_files.len());
    found.stack_line = stack_line(&deps, &all_paths, &paths, &found.facts);
    found
}

/// "Found 412 .tsx files and react 18 in `web/package.json`." A weak one adds why it isn't enough.
fn reason(evs: &[Ev], strong: bool) -> Vec<Part> {
    let build = |n: usize| {
        let mut parts = vec![t("Found ")];
        for (i, e) in evs.iter().take(n).enumerate() {
            if i > 0 {
                parts.push(t(" and "));
            }
            parts.extend(e.phrase.iter().cloned());
        }
        parts.push(t("."));
        if !strong {
            if let Some(why) = evs.first().and_then(|e| e.why) {
                parts.push(t(format!(" {why}")));
            }
        }
        merge(parts)
    };
    let two = build(2);
    if parts_text(&two).chars().count() <= MAX_REASON {
        two
    } else {
        build(1)
    }
}

/// Joins neighbouring text parts.
fn merge(parts: Vec<Part>) -> Vec<Part> {
    let mut out: Vec<Part> = Vec::new();
    for p in parts {
        match (out.last_mut(), p) {
            (Some(Part::Text(a)), Part::Text(b)) => a.push_str(&b),
            (_, p) => out.push(p),
        }
    }
    out
}

fn facts(deps: &[Dep], entries: &[Entry], counted: &[&str], top_ext: Option<(&str, usize)>, ui_files: usize) -> Vec<Fact> {
    let mut out: Vec<Fact> = Vec::new();
    let mut push = |name: String, detail: String| {
        if !out.iter().any(|f| f.name == name) && out.len() < 16 {
            out.push(Fact { name, detail });
        }
    };
    let shown = |p: &str| show_path(p).unwrap_or_else(|| file_name(p).chars().filter(|c| c.is_ascii_alphanumeric() || ".-_".contains(*c)).take(MAX_NAME).collect());
    // A dev dependency is a fact when a reason can cite it (N-1): tests, build tools, Storybook,
    // and the only find of its kind ("It's only in devDependencies").
    let cited = |d: &Dep| !d.dev || matches!(d.known.tag, Test | Build | Story) || !deps.iter().any(|o| !o.dev && o.known.tag == d.known.tag);
    for d in deps.iter().filter(|d| cited(d)) {
        let name = match d.major {
            Some(m) => format!("{} {m}", d.known.label),
            None => d.known.label.to_string(),
        };
        push(name, format!("{} in {}", d.known.name.trim_start_matches("Sdk:"), shown(&d.path)));
    }
    if let Some((e, n)) = top_ext {
        let name = match e {
            "tsx" => "TypeScript",
            "jsx" => "JSX",
            "vue" => "Vue",
            _ => "Svelte",
        };
        let _ = ui_files;
        push(name.to_string(), plural(n, &format!(".{e} file"), &format!(".{e} files")));
    }
    let mut all: Vec<&str> = entries.iter().map(|e| e.path.as_str()).collect();
    all.sort_unstable();
    let first = |m: Manifest| all.iter().copied().find(|p| manifest_kind(p) == Some(m));
    let count = |m: Manifest| all.iter().filter(|p| manifest_kind(p) == Some(m)).count();
    for (m, name) in [
        (Manifest::CargoToml, "Rust"),
        (Manifest::GoMod, "Go"),
        (Manifest::Pyproject, "Python"),
        (Manifest::Requirements, "Python"),
        (Manifest::Pom, "Java"),
        (Manifest::Gradle, "Java"),
        (Manifest::Csproj, "C#"),
        (Manifest::Composer, "PHP"),
        (Manifest::Gemfile, "Ruby"),
        (Manifest::Pubspec, "Flutter"),
        (Manifest::PackageSwift, "Swift"),
        (Manifest::Dockerfile, "Docker"),
        (Manifest::Compose, "Docker"),
        (Manifest::Dbt, "dbt"),
        (Manifest::Mkdocs, "MkDocs"),
        (Manifest::Docusaurus, "Docusaurus"),
        (Manifest::GitlabCi, "GitLab CI"),
    ] {
        if let Some(p) = first(m) {
            push(name.to_string(), shown(p));
        }
    }
    if count(Manifest::Workflow) > 0 {
        push("GitHub Actions".into(), plural(count(Manifest::Workflow), "workflow", "workflows"));
    }
    if count(Manifest::Terraform) > 0 {
        push("Terraform".into(), plural(count(Manifest::Terraform), ".tf file", ".tf files"));
    }
    let sql = counted.iter().filter(|p| ext(p) == Some("sql") || in_dir(p, &["migrations"])).count();
    if sql > 0 {
        push("SQL".into(), plural(sql, "migration", "migrations"));
    }
    let nb = counted.iter().filter(|p| ext(p) == Some("ipynb")).count();
    if nb > 0 {
        push("Jupyter".into(), plural(nb, "notebook", "notebooks"));
    }
    let tests = counted.iter().filter(|p| is_test_file(p)).count();
    if tests > 0 {
        push("Tests".into(), plural(tests, "test file", "test files"));
    }
    out
}

/// D5: "This team's code: Rust (axum), React 18 + TypeScript (Vite), Docker". Only [`KNOWN`]
/// labels and fixed words.
fn stack_line(deps: &[Dep], all: &[&str], counted: &[&str], facts: &[Fact]) -> Option<String> {
    let has = |m: Manifest| all.iter().any(|p| manifest_kind(p) == Some(m));
    let label_of = |eco: Eco, tag: Tag| deps.iter().find(|d| d.known.eco == eco && d.known.tag == tag && !d.dev).map(|d| d.known.label);
    let mut items: Vec<String> = Vec::new();
    let lang = |items: &mut Vec<String>, present: bool, name: &str, fw: Option<&str>| {
        if present {
            items.push(match fw {
                Some(f) => format!("{name} ({f})"),
                None => name.to_string(),
            });
        }
    };
    lang(&mut items, has(Manifest::CargoToml), "Rust", label_of(Cargo, Server));
    lang(&mut items, has(Manifest::GoMod), "Go", label_of(Go, Server));
    lang(&mut items, has(Manifest::Pyproject) || has(Manifest::Requirements), "Python", label_of(Py, Server));
    lang(&mut items, label_of(Npm, Server).is_some(), "Node.js", label_of(Npm, Server));
    lang(&mut items, has(Manifest::Pom) || has(Manifest::Gradle), "Java", label_of(Maven, Server).or(label_of(Gradle, Server)));
    lang(&mut items, has(Manifest::Csproj), "C#", label_of(Nuget, Server));
    lang(&mut items, has(Manifest::Composer), "PHP", label_of(Composer, Server));
    lang(&mut items, has(Manifest::Gemfile), "Ruby", label_of(Ruby, Server));
    if let Some(ui) = deps.iter().find(|d| d.known.tag == Ui) {
        let mut s = ui.known.label.to_string();
        if let Some(m) = ui.major {
            s.push_str(&format!(" {m}"));
        }
        let ts_files = counted.iter().filter(|p| matches!(ext(p), Some("ts" | "tsx"))).count();
        if ts_files >= 10 || deps.iter().any(|d| d.known.name == "typescript" && d.known.eco == Npm) {
            s.push_str(" + TypeScript");
        }
        if let Some(b) = deps.iter().find(|d| d.known.tag == Build && d.known.name != "typescript") {
            s.push_str(&format!(" ({})", b.known.label));
        }
        items.push(s);
    }
    lang(&mut items, has(Manifest::Pubspec), "Flutter", None);
    lang(&mut items, has(Manifest::PackageSwift), "Swift", None);
    for name in ["Docker", "Terraform", "dbt", "Jupyter"] {
        if facts.iter().any(|f| f.name == name) {
            items.push(name.to_string());
        }
    }
    items.dedup();
    items.truncate(8);
    (!items.is_empty()).then(|| format!("This team's code: {}", items.join(", ")))
}

// ---------------------------------------------------------------------------------------------
// The roster (D3, A13, Q-D2)

/// One suggested bot (a row of the sheet).
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Row {
    /// `lead` for the lead, else the preset id (or the Coding template slot in a fallback).
    pub key: String,
    pub preset: String,
    pub lead: bool,
    pub name: String,
    pub title: String,
    pub avatar_sprite: String,
    pub reason_parts: Vec<Part>,
    pub reason: String,
    pub owns: Vec<String>,
    pub existing_kwant_id: Option<String>,
    pub existing_name: Option<String>,
}

pub const LEAD_NAME: &str = "TechLead";

fn row(key: &str, role: Role, lead: bool, reason: Vec<Part>, owns: Vec<String>) -> Row {
    let p = role.preset();
    let (name, title) = match (lead, role) {
        (true, Role::Frontend) => (LEAD_NAME.to_string(), "Tech Lead · Frontend".to_string()),
        (true, _) => (LEAD_NAME.to_string(), "Tech Lead · Backend".to_string()),
        (false, _) => (p.name.clone(), p.title.clone()),
    };
    Row {
        key: key.to_string(),
        preset: p.id.clone(),
        lead,
        name,
        title,
        avatar_sprite: p.avatar_sprite.clone(),
        reason: parts_text(&reason),
        reason_parts: reason,
        owns,
        existing_kwant_id: None,
        existing_name: None,
    }
}

const FROM_CODING: &str = "From the Coding template.";

/// Who to suggest.
#[derive(Debug, Clone)]
pub enum Mode {
    /// A new team: a lead and 2 to 6 bots.
    New,
    /// More members for a team (D1 Manage team, A18): only roles it doesn't have, no lead.
    AddTo { have: Vec<Role> },
}

#[derive(Debug, Clone)]
pub struct Roster {
    pub bots: Vec<Row>,
    pub also_possible: Vec<Row>,
    pub fallback: bool,
}

pub fn roster(found: &Found, mode: &Mode) -> Roster {
    let have: &[Role] = match mode {
        Mode::AddTo { have } => have,
        Mode::New => &[],
    };
    let mut strong: Vec<&RoleFind> = found.roles.values().filter(|r| r.strong && !have.contains(&r.role)).collect();
    strong.sort_by(|a, b| b.score.cmp(&a.score).then(a.role.cmp(&b.role)));
    let weak: Vec<&RoleFind> = found.roles.values().filter(|r| !r.strong && !have.contains(&r.role)).collect();
    let also_row = |r: &RoleFind| row(r.role.preset_id(), r.role, false, r.reason.clone(), r.owns.clone());
    let over = |r: &RoleFind| {
        let mut reason = r.reason.clone();
        reason.push(t(" 6 bots at most."));
        let reason = merge(reason);
        row(r.role.preset_id(), r.role, false, reason, r.owns.clone())
    };

    if let Mode::AddTo { .. } = mode {
        let bots = strong.iter().take(MAX_BOTS).map(|r| also_row(r)).collect();
        let mut also: Vec<Row> = strong.iter().skip(MAX_BOTS).map(|r| over(r)).collect();
        also.extend(weak.iter().map(|r| also_row(r)));
        return Roster { bots, also_possible: also, fallback: false };
    }

    if strong.is_empty() {
        // D7: the Coding template's roster; weak finds stay under Also possible
        let coding = super::templates::get("coding").expect("the Coding template exists");
        let mut bots = Vec::new();
        for b in &coding.bots {
            let role = Role::from_preset(&b.preset).unwrap_or(Role::Backend);
            let key = if b.lead { "lead".to_string() } else { b.name.to_ascii_lowercase() };
            let mut r = row(&key, role, b.lead, vec![t(FROM_CODING)], Vec::new());
            if !b.lead {
                r.name = b.name.clone();
                r.title = b.title.clone();
            }
            r.avatar_sprite = b.avatar_sprite.clone();
            bots.push(r);
        }
        let keys: BTreeSet<String> = bots.iter().map(|b| b.key.clone()).collect();
        let also = weak.iter().filter(|r| !keys.contains(r.role.preset_id())).map(|r| also_row(r)).collect();
        return Roster { bots, also_possible: also, fallback: true };
    }

    let backend = found.roles.get(&Role::Backend);
    let lead_role = if backend.is_some_and(|r| r.strong) {
        Role::Backend
    } else if found.roles.get(&Role::Frontend).is_some_and(|r| r.strong) && backend.is_none() {
        Role::Frontend
    } else {
        Role::Backend
    };
    let lead = match found.roles.get(&lead_role).filter(|r| r.strong) {
        Some(r) => row("lead", lead_role, true, r.reason.clone(), r.owns.clone()),
        None => row("lead", Role::Backend, true, vec![t("Leads the team, as in the Coding template.")], Vec::new()),
    };
    let members: Vec<&RoleFind> = strong.iter().copied().filter(|r| r.role != lead_role).collect();
    let mut bots = vec![lead];
    bots.extend(members.iter().take(MAX_BOTS - 1).map(|r| also_row(r)));
    let mut also: Vec<Row> = members.iter().skip(MAX_BOTS - 1).map(|r| over(r)).collect();
    also.extend(weak.iter().filter(|r| r.role != lead_role).map(|r| also_row(r)));
    // Q-D2b: a lone find is padded to 2, from the Coding roster
    for pad in [Role::Qa, Role::Backend, Role::Frontend] {
        if bots.len() >= MIN_BOTS {
            break;
        }
        if bots.iter().all(|b| b.preset != pad.preset_id() || b.lead) && pad != lead_role {
            also.retain(|a| a.key != pad.preset_id());
            bots.push(row(pad.preset_id(), pad, false, vec![t(FROM_CODING)], Vec::new()));
        }
    }
    Roster { bots, also_possible: also, fallback: false }
}

/// D6 / Q-D4b: each row's oldest matching bot in the space (by name or title, ignoring case;
/// `bots` oldest first), each bot offered for one row at most.
pub fn match_existing(rows: &mut [&mut Row], bots: &[(String, String, String)]) {
    let mut used = BTreeSet::new();
    for r in rows.iter_mut() {
        let preset = presets::get(&r.preset);
        let hit = bots.iter().find(|(id, name, title)| {
            !used.contains(id)
                && (name.eq_ignore_ascii_case(&r.name)
                    || title.eq_ignore_ascii_case(&r.title)
                    || preset.is_some_and(|p| !r.lead && (name.eq_ignore_ascii_case(&p.name) || title.eq_ignore_ascii_case(&p.title))))
        });
        if let Some((id, name, _)) = hit {
            used.insert(id.clone());
            r.existing_kwant_id = Some(id.clone());
            r.existing_name = Some(name.clone());
        }
    }
}

/// The roles a team already has (D-R8): a member whose name or title is a role preset's.
pub fn roles_of(members: &[(String, String)]) -> Vec<Role> {
    ROLES
        .into_iter()
        .filter(|r| {
            let p = r.preset();
            members.iter().any(|(name, title)| {
                name.eq_ignore_ascii_case(&p.name)
                    || title.eq_ignore_ascii_case(&p.title)
                    || (*r == Role::Backend && (name.eq_ignore_ascii_case(LEAD_NAME) || title.starts_with("Tech Lead")))
            })
        })
        .collect()
}

// ---------------------------------------------------------------------------------------------
// What a created bot gets (D5, Q-D11)

const LEAD_LINE: &str = "You lead this team: you turn each request into a small plan with a \"done when\" line, split the work between the members, and make sure every change is tested before it's called done.";

/// A new bot's persona: its preset's, the lead line for the lead, then the stack line and its
/// folders as data (D5, Q-D10). Nothing else from the repo.
pub fn persona(preset: &Preset, lead: bool, stack_line: Option<&str>, owns: &[String]) -> String {
    let mut s = preset.persona_prompt.trim_end().to_string();
    if lead {
        s.push('\n');
        s.push_str(LEAD_LINE);
    }
    if let Some(line) = stack_line {
        s.push_str("\n\n");
        s.push_str(line);
        s.push('.');
    }
    let folders = folders_line(owns);
    if !folders.is_empty() {
        s.push_str(if stack_line.is_some() { "\n" } else { "\n\n" });
        s.push_str(&folders);
    }
    s
}

/// The one sentence a persona may name folders in, each as code (Q-D10, D-I4):
/// ``You look after `src/api` and `migrations`.`` A name that fails [`valid_folder`] is dropped;
/// with none left there is no sentence.
pub fn folders_line(owns: &[String]) -> String {
    let list: Vec<String> = owns
        .iter()
        .map(|f| f.trim_end_matches('/'))
        .filter(|f| !f.is_empty() && f.split('/').all(valid_folder))
        .map(|f| format!("`{f}`"))
        .collect();
    match list.as_slice() {
        [] => String::new(),
        [one] => format!("You look after {one}."),
        [rest @ .., last] => format!("You look after {} and {last}.", rest.join(", ")),
    }
}

/// A suggested team's pinned instructions (Q-D11, A16): fixed text, the stack line, and the
/// Coding template's "How we work" and "Never without the person".
pub fn team_instructions(stack_line: Option<&str>) -> String {
    let coding = super::templates::get("coding").expect("the Coding template exists");
    let rules = coding.instructions.split_once("\n\n").map(|(_, rest)| rest).unwrap_or(&coding.instructions);
    let mut s = String::from("This team builds changes in one project.\nThe lead turns a request into a small plan and splits the work; each member works in its own role.");
    if let Some(line) = stack_line {
        s.push('\n');
        s.push_str(line);
        s.push('.');
    }
    s.push_str("\n\n");
    s.push_str(rules);
    s
}

/// The default handle (Q-D5): the folder's or repo's base name, as a handle.
pub fn default_handle(source: &str) -> String {
    let base = source.trim_end_matches('/').rsplit(['/', ':']).next().unwrap_or("");
    let base = base.strip_suffix(".git").unwrap_or(base);
    super::names::slug(base).unwrap_or_else(|| "coding".to_string())
}

#[cfg(test)]
mod tests;