knowyourbusiness 0.3.1

Git-backed, searchable knowledge base and incident tracker for AI agent fleets
use crate::AppState;
use axum::extract::{ConnectInfo, Request, State};
use axum::middleware::Next;
use axum::response::Response;
use std::io::Write;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::{Arc, Mutex};

/// Append-only JSONL audit: who (ip), what (method/path/query), how it went
/// (status), how long it took (ms). A request-audit log without the
/// machinery — traffic here is tiny, single-user.
pub struct Audit {
    file: Mutex<std::fs::File>,
}

impl Audit {
    pub fn open(path: &Path) -> anyhow::Result<Audit> {
        let file = std::fs::OpenOptions::new().create(true).append(true).open(path)?;
        Ok(Audit { file: Mutex::new(file) })
    }

    fn append(&self, line: &serde_json::Value) {
        if let Ok(mut f) = self.file.lock() {
            let _ = writeln!(f, "{line}");
        }
    }
}

pub async fn audit_mw(State(st): State<Arc<AppState>>, req: Request, next: Next) -> Response {
    let path = req.uri().path().to_string();
    let query = req.uri().query().unwrap_or("").to_string();
    let method = req.method().to_string();

    // Skip polling & internal dashboard noise:
    // - healthz probe
    // - audit & activity log polling
    // - tag taxonomy fetching (/tags)
    // - background lists without filter (/incidents, /tasks with GET)
    // - empty search polling (/search without q parameter)
    let is_polling = path == "/healthz"
        || path == "/api/audit"
        || path == "/api/activity"
        || path == "/tags"
        || ((path == "/incidents" || path == "/tasks") && method == "GET")
        || (path == "/search" && !query.contains("q="));

    if is_polling {
        return next.run(req).await;
    }

    let ts = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true);
    let ip = req
        .extensions()
        .get::<ConnectInfo<SocketAddr>>()
        .map(|c| c.0.ip().to_string())
        .unwrap_or_else(|| "-".into());
    let start = std::time::Instant::now();
    let resp = next.run(req).await;
    st.audit.append(&serde_json::json!({
        "ts": ts,
        "ip": ip,
        "method": method,
        "path": path,
        "query": query,
        "status": resp.status().as_u16(),
        "ms": start.elapsed().as_millis() as u64,
        "actor": resp.extensions().get::<crate::auth::ActorTag>().map(|t| t.0.clone()).unwrap_or_else(|| "-".into()),
        "by": resp.extensions().get::<crate::auth::ByTag>().and_then(|t| t.0.clone()),
    }));
    resp
}