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    /// Fresh-launch migration policy for deprecated `profile.worker_binding`.
30    pub legacy_worker_binding_policy: mlua_swarm::LegacyWorkerBindingPolicy,
31    /// Seed blueprint id (= combined mode default).
32    pub seed_blueprint_id: String,
33    /// Server-wide [`mlua_swarm::core::config::CheckPolicy`] resolved
34    /// from CLI flag > config file > built-in default (`Warn`). See
35    /// `mlua_swarm_server::config::ResolvedConfig.check_policy` for the
36    /// full cascade. Serialised as snake_case (`"silent"` / `"warn"` /
37    /// `"strict"`).
38    pub check_policy: mlua_swarm::core::config::CheckPolicy,
39}
40
41#[derive(Clone)]
42struct DoctorState {
43    info: Arc<DoctorInfo>,
44    store: Arc<dyn BlueprintStore>,
45}
46
47/// Builds the `/v1/doctor` router. `info` is the (immutable) startup snapshot
48/// to serve; `store` is used to peek the registered Blueprint id count/list.
49pub fn build_doctor_router(info: DoctorInfo, store: Arc<dyn BlueprintStore>) -> Router {
50    let state = DoctorState {
51        info: Arc::new(info),
52        store,
53    };
54    Router::new()
55        .route("/v1/doctor", get(doctor_get))
56        .with_state(state)
57}
58
59#[derive(Serialize)]
60struct DoctorResponse {
61    #[serde(flatten)]
62    info: DoctorInfo,
63    /// Registered BP id list (best-effort; currently returns empty for the InMemory backend).
64    registered_blueprint_ids: Vec<String>,
65    registered_blueprint_count: usize,
66}
67
68async fn doctor_get(State(state): State<DoctorState>) -> Json<DoctorResponse> {
69    // `store.list_ids()` applies the archive filter (archived ids are excluded by default).
70    // The InMemory backend is expected to return Ok(vec![]).
71    let mut ids: Vec<String> = state
72        .store
73        .list_ids()
74        .await
75        .map(|v| v.into_iter().map(|id| id.to_string()).collect())
76        .unwrap_or_default();
77    ids.sort();
78    let count = ids.len();
79    Json(DoctorResponse {
80        info: (*state.info).clone(),
81        registered_blueprint_ids: ids,
82        registered_blueprint_count: count,
83    })
84}