Skip to main content

mlua_swarm_server/
doctor.rs

1//! `GET /v1/doctor` — server-side infra info snapshot.
2//!
3//! Surfaces startup config read-only: the Blueprint store real (backend /
4//! root path), ref_base, bind, enhance flow on/off, etc. An entry point for
5//! callers (the MCP adapter's doctor tool / operator) to answer "where is the Store?"
6//! and "how many BPs are registered?" in one shot.
7//!
8//! Store contents (BP list / head / history) are peeked via the existing
9//! `/v1/blueprints/...` routes; doctor covers only the infra layer.
10
11use axum::{extract::State, routing::get, Json, Router};
12use mlua_swarm::blueprint::store::BlueprintStore;
13use serde::Serialize;
14use std::sync::Arc;
15
16/// Startup config snapshot. Populated from `Args` in `main.rs` and mounted on the router.
17#[derive(Clone, Serialize)]
18pub struct DoctorInfo {
19    /// Listen address (`--bind` value).
20    pub bind: String,
21    /// Backend type: `"git2"` | `"in_memory"`.
22    pub blueprint_backend: String,
23    /// Git backend root (Git2 only). `None` for InMemory.
24    pub blueprint_store_root: Option<String>,
25    /// `--blueprint-ref-base` (= base dir for `$agent_md` / `$file` expansion).
26    pub blueprint_ref_base: Option<String>,
27    /// `--enable-enhance-flow` on/off.
28    pub enhance_flow_enabled: bool,
29    /// Seed blueprint id (= combined mode default).
30    pub seed_blueprint_id: String,
31    /// Server-wide [`mlua_swarm::core::config::CheckPolicy`] resolved
32    /// from CLI flag > config file > built-in default (`Warn`). See
33    /// `mlua_swarm_server::config::ResolvedConfig.check_policy` for the
34    /// full cascade. Serialised as snake_case (`"silent"` / `"warn"` /
35    /// `"strict"`).
36    pub check_policy: mlua_swarm::core::config::CheckPolicy,
37}
38
39#[derive(Clone)]
40struct DoctorState {
41    info: Arc<DoctorInfo>,
42    store: Arc<dyn BlueprintStore>,
43}
44
45/// Builds the `/v1/doctor` router. `info` is the (immutable) startup snapshot
46/// to serve; `store` is used to peek the registered Blueprint id count/list.
47pub fn build_doctor_router(info: DoctorInfo, store: Arc<dyn BlueprintStore>) -> Router {
48    let state = DoctorState {
49        info: Arc::new(info),
50        store,
51    };
52    Router::new()
53        .route("/v1/doctor", get(doctor_get))
54        .with_state(state)
55}
56
57#[derive(Serialize)]
58struct DoctorResponse {
59    #[serde(flatten)]
60    info: DoctorInfo,
61    /// Registered BP id list (best-effort; currently returns empty for the InMemory backend).
62    registered_blueprint_ids: Vec<String>,
63    registered_blueprint_count: usize,
64}
65
66async fn doctor_get(State(state): State<DoctorState>) -> Json<DoctorResponse> {
67    // `store.list_ids()` applies the archive filter (archived ids are excluded by default).
68    // The InMemory backend is expected to return Ok(vec![]).
69    let mut ids: Vec<String> = state
70        .store
71        .list_ids()
72        .await
73        .map(|v| v.into_iter().map(|id| id.to_string()).collect())
74        .unwrap_or_default();
75    ids.sort();
76    let count = ids.len();
77    Json(DoctorResponse {
78        info: (*state.info).clone(),
79        registered_blueprint_ids: ids,
80        registered_blueprint_count: count,
81    })
82}