ironclad-api 0.9.8

HTTP routes, WebSocket, auth, rate limiting, and dashboard for the Ironclad agent runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415

#[derive(Deserialize)]
pub struct RegisterDiscoveredAgentRequest {
    pub agent_id: String,
    pub name: String,
    pub url: String,
    #[serde(default)]
    pub capabilities: Vec<String>,
}

#[derive(Deserialize)]
pub struct PairDeviceRequest {
    pub device_id: String,
    pub public_key_hex: String,
    pub device_name: String,
}

pub async fn get_runtime_surfaces(State(state): State<AppState>) -> impl IntoResponse {
    let discovery = state.discovery.read().await;
    let devices = state.devices.read().await;
    let mcp_clients = state.mcp_clients.read().await;
    let mcp_server = state.mcp_server.read().await;
    Json(json!({
        "discovery": {
            "count": discovery.count(),
            "verified_count": discovery.verified_agents().len(),
        },
        "devices": {
            "device_id": devices.identity().device_id,
            "fingerprint": devices.identity().fingerprint(),
            "paired_count": devices.paired_count(),
            "trusted_count": devices.trusted_devices().len(),
        },
        "mcp": {
            "server_enabled": true,
            "tools_exposed": mcp_server.tool_count(),
            "resources_exposed": mcp_server.resource_count(),
            "client_total": mcp_clients.total_count(),
            "client_connected": mcp_clients.connected_count(),
        }
    }))
}

pub async fn list_discovered_agents(State(state): State<AppState>) -> impl IntoResponse {
    let discovery = state.discovery.read().await;
    let agents: Vec<_> = discovery
        .all_agents()
        .iter()
        .map(|a| {
            json!({
                "agent_id": a.agent_id,
                "name": a.name,
                "url": a.url,
                "capabilities": a.capabilities,
                "verified": a.verified,
                "discovery_method": format!("{}", a.discovery_method),
                "last_seen": a.last_seen,
            })
        })
        .collect();
    Json(json!({ "agents": agents, "count": agents.len() }))
}

pub async fn register_discovered_agent(
    State(state): State<AppState>,
    Json(body): Json<RegisterDiscoveredAgentRequest>,
) -> Result<impl IntoResponse, JsonError> {
    validate_short("agent_id", &body.agent_id)?;
    validate_short("name", &body.name)?;
    validate_short("url", &body.url)?;
    let body = RegisterDiscoveredAgentRequest {
        name: sanitize_html(&body.name),
        ..body
    };
    let mut discovery = state.discovery.write().await;
    discovery.register(ironclad_agent::discovery::DiscoveredAgent {
        agent_id: body.agent_id.clone(),
        name: body.name,
        url: body.url,
        capabilities: body.capabilities,
        verified: false,
        discovered_at: chrono::Utc::now(),
        last_seen: chrono::Utc::now(),
        discovery_method: ironclad_agent::discovery::DiscoveryMethod::Manual,
    });
    Ok(Json(json!({ "ok": true, "agent_id": body.agent_id })))
}

pub async fn verify_discovered_agent(
    State(state): State<AppState>,
    Path(agent_id): Path<String>,
) -> impl IntoResponse {
    let mut discovery = state.discovery.write().await;
    match discovery.verify(&agent_id) {
        Ok(()) => (
            StatusCode::OK,
            Json(json!({ "ok": true, "agent_id": agent_id })),
        )
            .into_response(),
        Err(e) => (
            StatusCode::NOT_FOUND,
            Json(json!({ "ok": false, "error": e.to_string() })),
        )
            .into_response(),
    }
}

pub async fn list_paired_devices(State(state): State<AppState>) -> impl IntoResponse {
    let devices = state.devices.read().await;
    let device_list: Vec<_> = devices
        .all_devices()
        .iter()
        .map(|d| {
            json!({
                "device_id": d.device_id,
                "device_name": d.device_name,
                "state": format!("{:?}", d.state).to_lowercase(),
                "paired_at": d.paired_at,
                "last_seen": d.last_seen,
            })
        })
        .collect();
    Json(json!({
        "identity": {
            "device_id": devices.identity().device_id,
            "public_key_hex": devices.identity().public_key_hex,
            "fingerprint": devices.identity().fingerprint(),
        },
        "devices": device_list,
    }))
}

pub async fn pair_device(
    State(state): State<AppState>,
    Json(body): Json<PairDeviceRequest>,
) -> impl IntoResponse {
    let mut devices = state.devices.write().await;
    match devices.initiate_pairing(&body.device_id, &body.public_key_hex, &body.device_name) {
        Ok(()) => (
            StatusCode::OK,
            Json(json!({"ok": true, "device_id": body.device_id})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::BAD_REQUEST,
            Json(json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
    }
}

pub async fn verify_paired_device(
    State(state): State<AppState>,
    Path(device_id): Path<String>,
) -> impl IntoResponse {
    let mut devices = state.devices.write().await;
    match devices.verify_pairing(&device_id) {
        Ok(()) => (
            StatusCode::OK,
            Json(json!({"ok": true, "device_id": device_id})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::BAD_REQUEST,
            Json(json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
    }
}

pub async fn unpair_device(
    State(state): State<AppState>,
    Path(device_id): Path<String>,
) -> impl IntoResponse {
    let mut devices = state.devices.write().await;
    match devices.unpair(&device_id) {
        Ok(()) => (
            StatusCode::OK,
            Json(json!({"ok": true, "device_id": device_id})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::NOT_FOUND,
            Json(json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
    }
}

pub async fn get_mcp_runtime(State(state): State<AppState>) -> impl IntoResponse {
    let clients = state.mcp_clients.read().await;
    let server = state.mcp_server.read().await;
    let connections: Vec<_> = clients
        .list_connections()
        .iter()
        .map(|c| {
            json!({
                "name": c.name,
                "url": c.url,
                "connected": c.connected,
                "tools": c.available_tools.len(),
                "resources": c.available_resources.len(),
            })
        })
        .collect();
    let tools: Vec<_> = server
        .list_tools()
        .iter()
        .map(|t| json!({"name": t.name, "description": t.description}))
        .collect();
    let resources: Vec<_> = server
        .list_resources()
        .iter()
        .map(|r| json!({"uri": r.uri, "name": r.name}))
        .collect();
    Json(json!({
        "connections": connections,
        "connected_count": clients.connected_count(),
        "exposed_tools": tools,
        "exposed_resources": resources,
    }))
}

pub async fn mcp_client_discover(
    State(state): State<AppState>,
    Path(name): Path<String>,
) -> impl IntoResponse {
    let mut clients = state.mcp_clients.write().await;
    match clients.get_connection_mut(&name) {
        Some(conn) => match conn.discover() {
            Ok(()) => (StatusCode::OK, Json(json!({"ok": true, "name": name}))).into_response(),
            Err(e) => (
                StatusCode::BAD_GATEWAY,
                Json(json!({"ok": false, "error": e.to_string()})),
            )
                .into_response(),
        },
        None => (
            StatusCode::NOT_FOUND,
            Json(json!({"ok": false, "error": "mcp client not found"})),
        )
            .into_response(),
    }
}

pub async fn mcp_client_disconnect(
    State(state): State<AppState>,
    Path(name): Path<String>,
) -> impl IntoResponse {
    let mut clients = state.mcp_clients.write().await;
    match clients.get_connection_mut(&name) {
        Some(conn) => {
            conn.disconnect();
            (StatusCode::OK, Json(json!({"ok": true, "name": name}))).into_response()
        }
        None => (
            StatusCode::NOT_FOUND,
            Json(json!({"ok": false, "error": "mcp client not found"})),
        )
            .into_response(),
    }
}

// ── WebSocket ticket issuance ─────────────────────────────────

pub async fn issue_ws_ticket(State(state): State<AppState>) -> impl IntoResponse {
    let ticket = state.ws_tickets.issue();
    Json(json!({ "ticket": ticket, "expires_in": 30 }))
}

/// GET /api/models/routing-diagnostics
///
/// Returns a comprehensive snapshot of routing state for operator diagnostics:
/// - Model profiles with metascores at current complexity
/// - Circuit breaker states
/// - Shadow prediction agreement summary
/// - Active routing config (accuracy floor, cost weight, canary, blocklist)
pub async fn get_routing_diagnostics(State(state): State<AppState>) -> impl IntoResponse {
    let config = state.config.read().await;
    let routing_config = &config.models.routing;
    let cost_aware = routing_config.cost_aware;
    let cost_weight = routing_config.cost_weight;
    let accuracy_floor = routing_config.accuracy_floor;
    let accuracy_min_obs = routing_config.accuracy_min_obs;
    let canary_model = routing_config.canary_model.clone();
    let canary_fraction = routing_config.canary_fraction;
    let blocked_models = routing_config.blocked_models.clone();
    let routing_mode = routing_config.mode.clone();
    drop(config);

    let llm_read = state.llm.read().await;

    // Build profiles for all configured models.
    let profiles = ironclad_llm::build_model_profiles(
        &llm_read.router,
        &llm_read.providers,
        &llm_read.quality,
        &llm_read.capacity,
        &llm_read.breakers,
    );

    // Trace-backed confidence inputs from executed turns (selected model -> observed quality).
    let trace_quality_by_model: HashMap<String, (i64, Option<f64>)> = {
        let conn = state.db.conn();
        let mut map = HashMap::new();
        if let Ok(mut stmt) = conn.prepare(
            "SELECT mse.selected_model, COUNT(*) AS obs, AVG(ic.quality_score) AS avg_quality
             FROM model_selection_events mse
             INNER JOIN inference_costs ic ON ic.turn_id = mse.turn_id
             WHERE ic.quality_score IS NOT NULL
             GROUP BY mse.selected_model",
        ) && let Ok(rows) = stmt.query_map([], |r| {
            Ok((
                r.get::<_, String>(0)?,
                r.get::<_, i64>(1)?,
                r.get::<_, Option<f64>>(2)?,
            ))
        }) {
            for (model, obs, avg_quality) in rows.flatten() {
                map.insert(model, (obs, avg_quality));
            }
        }
        map
    };

    // Compute metascores at a representative complexity (0.5 = medium).
    let profile_diagnostics: Vec<Value> = profiles
        .iter()
        .map(|p| {
            let breakdown = p.metascore_with_cost_weight(0.5, cost_aware, cost_weight);
            let (trace_obs, trace_avg_quality) = trace_quality_by_model
                .get(&p.model_name)
                .copied()
                .unwrap_or((0, None));
            let confidence_trace_backed =
                trace_obs >= accuracy_min_obs as i64 && trace_avg_quality.is_some();
            let confidence = if confidence_trace_backed {
                let observed = trace_avg_quality.unwrap_or(0.0).clamp(0.0, 1.0);
                Some(((breakdown.confidence + observed) / 2.0).clamp(0.0, 1.0))
            } else {
                None
            };
            json!({
                "model": p.model_name,
                "is_local": p.is_local,
                "tier": format!("{:?}", p.tier),
                "cost_per_1k_tokens": (p.cost_per_input_token + p.cost_per_output_token) * 1000.0,
                "estimated_quality": p.estimated_quality,
                "observation_count": p.observation_count,
                "availability": p.availability,
                "capacity_headroom": p.capacity_headroom,
                "metascore": {
                    "final_score": breakdown.final_score,
                    "efficacy": breakdown.efficacy,
                    "cost": breakdown.cost,
                    "availability": breakdown.availability,
                    "locality": breakdown.locality,
                    "confidence": confidence,
                    "confidence_raw": breakdown.confidence,
                    "confidence_trace_backed": confidence_trace_backed,
                    "confidence_trace_observations": trace_obs,
                    "confidence_trace_avg_quality": trace_avg_quality,
                },
                "blocked_by_config": blocked_models.contains(&p.model_name),
            })
        })
        .collect();

    // Circuit breaker states.
    let breaker_states: Vec<Value> = llm_read
        .breakers
        .list_providers()
        .into_iter()
        .map(|(name, state)| {
            json!({
                "provider": name,
                "state": format!("{state:?}"),
                "credit_tripped": llm_read.breakers.is_credit_tripped(&name),
                "operator_forced_open": llm_read.breakers.is_operator_forced_open(&name),
            })
        })
        .collect();

    // Shadow prediction summary (if any data exists).
    let shadow_summary =
        ironclad_db::shadow_routing::shadow_agreement_summary(&state.db, None)
            .inspect_err(|e| tracing::warn!(error = %e, "failed to load shadow agreement summary"))
            .ok();

    let shadow_json = shadow_summary.map(|s| {
        json!({
            "total": s.total,
            "agreed": s.agreed,
            "disagreed": s.disagreed,
            "agreement_rate": s.agreement_rate,
        })
    });

    Json(json!({
        "routing_mode": routing_mode,
        "config": {
            "cost_aware": cost_aware,
            "cost_weight": cost_weight,
            "accuracy_floor": accuracy_floor,
            "accuracy_min_obs": accuracy_min_obs,
            "canary_model": canary_model,
            "canary_fraction": canary_fraction,
            "blocked_models": blocked_models,
        },
        "profiles": profile_diagnostics,
        "circuit_breakers": breaker_states,
        "shadow_predictions": shadow_json,
    }))
}