File size: 13,094 Bytes
4ae5926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// http/mod.rs β€” axum REST router + SSE orchestration stream
//
// Endpoints:
//   POST /api/v1/orchestrate  β†’ SSE stream of BOB pipeline stages
//   POST /api/v1/verify       β†’ immediate ERRANT verification result
//   GET  /api/v1/chain/head   β†’ current WORM chain head
//   GET  /api/v1/health       β†’ daemon health
//   GET  /api/v1/version      β†’ liberrant + magmad version
//
// SSE stream format per stage:
//   data: {"stage":"TRUST-DEED-GATE","ok":true,"worm_hash":"...","message":"..."}
//
// The external REST/SSE surface is what the magma CLI, METAMINE bridge.mjs,
// and any web UI talks to. The internal gRPC surface talks agent-to-agent.

use axum::{
    extract::State,
    http::StatusCode,
    response::{
        sse::{Event, Sse},
        IntoResponse, Json,
    },
    routing::{get, post},
    Router,
};
use futures::stream::{self, Stream};
use serde::{Deserialize, Serialize};
use std::{convert::Infallible, sync::Arc, time::Duration};
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
use tracing::{error, info};

use crate::{bob, errant_ffi};

// ── Shared application state ───────────────────────────────────────────────

#[derive(Clone)]
pub struct AppState {
    pub config:     Arc<crate::config::Config>,
    pub nats:       Option<Arc<crate::nats_bus::NatsBus>>,
    pub worm_url:   String,
}

// ── Request / response types ───────────────────────────────────────────────

#[derive(Debug, Deserialize)]
pub struct OrchestrateRequest {
    pub session_id:  Option<String>,
    pub action:      String,
    pub code:        Option<String>,
    pub intent:      Option<String>,
    pub constraints: Option<Vec<String>>,
    pub trace_ops:   Option<Vec<String>>,
}

#[derive(Debug, Serialize)]
pub struct StageEvent {
    pub stage:      &'static str,
    pub ok:         bool,
    pub agent:      Option<String>,
    pub worm_hash:  Option<String>,
    pub proof_hash: Option<String>,
    pub message:    String,
    pub error:      Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct VerifyRequest {
    pub opcodes: Option<Vec<String>>,
    pub source:  Option<String>,
}

#[derive(Debug, Serialize)]
pub struct VerifyResponse {
    pub ok:           bool,
    pub verdict:      &'static str,
    pub worm_hash:    String,
    pub error:        Option<String>,
    pub steps:        i32,
    pub lin_consumed: i32,
    pub lin_leaked:   i32,
    pub fallback:     bool,
}

// ── Router ─────────────────────────────────────────────────────────────────

pub fn router(state: AppState) -> Router {
    Router::new()
        .route("/api/v1/orchestrate", post(orchestrate))
        .route("/api/v1/verify",      post(verify))
        .route("/api/v1/chain/head",  get(chain_head))
        .route("/api/v1/health",      get(health))
        .route("/api/v1/version",     get(version_handler))
        // Legacy MAGMA protocol endpoints (snap-os compatibility)
        .route("/api/labs/ledge/seal",post(legacy_seal))
        .route("/api/sovereign/dispatch", post(legacy_dispatch))
        .with_state(state)
}

// ── POST /api/v1/orchestrate ── SSE stream ─────────────────────────────────

async fn orchestrate(
    State(state): State<AppState>,
    Json(req): Json<OrchestrateRequest>,
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
    let (tx, rx) = mpsc::channel::<StageEvent>(16);

    tokio::spawn(async move {
        let send = |ev: StageEvent| {
            let tx = tx.clone();
            async move {
                let json = serde_json::to_string(&ev).unwrap_or_default();
                tx.send(ev).await.ok();
                json
            }
        };

        // ── Stage 1: TRUST-DEED-GATE ──────────────────────────────────────
        send(StageEvent {
            stage:     "TRUST-DEED-GATE",
            ok:        true,
            agent:     None,
            worm_hash: None,
            proof_hash: None,
            message:   "BOB gate open".into(),
            error:     None,
        }).await;

        // ── Stage 2: ERRANT linear type check ─────────────────────────────
        let ops: Vec<String> = req.trace_ops
            .or_else(|| req.code.as_ref().map(|s| {
                s.split_whitespace().map(String::from).collect()
            }))
            .unwrap_or_default();

        let op_refs: Vec<&str> = ops.iter().map(|s| s.as_str()).collect();
        let errant = if op_refs.is_empty() {
            errant_ffi::verify_source("PUSH_UN SEAL") // trivial pass for natural-language actions
        } else {
            errant_ffi::verify_named(&op_refs)
        };

        send(StageEvent {
            stage:     "ERRANT",
            ok:        errant.ok,
            agent:     None,
            worm_hash: Some(errant.worm_hash.clone()),
            proof_hash: None,
            message:   if errant.ok {
                format!("linear types verified Β· {} steps Β· {}", errant.steps,
                    if errant.fallback { "structural" } else { "full LFIS" })
            } else {
                format!("SILENCE: {}", errant.error)
            },
            error: if errant.ok { None } else { Some(errant.error.clone()) },
        }).await;

        if !errant.ok { return; }

        // ── Stage 3: MAMBA compress (stub) ────────────────────────────────
        send(StageEvent {
            stage: "MAMBA", ok: true, agent: None,
            worm_hash: Some(errant.worm_hash.clone()),
            proof_hash: None,
            message: "sequence compressed".into(), error: None,
        }).await;

        // ── Stage 4: WATSON attend (stub) ─────────────────────────────────
        send(StageEvent {
            stage: "WATSON", ok: true, agent: None,
            worm_hash: Some(errant.worm_hash.clone()),
            proof_hash: None,
            message: "attention applied".into(), error: None,
        }).await;

        // ── Stage 5: BOB routing ──────────────────────────────────────────
        let signals = bob::BobSignals::from_errant_and_action(&errant, &req.action);
        let route = bob::route(&signals);

        send(StageEvent {
            stage:     "BOB",
            ok:        true,
            agent:     Some(format!("{} {}", route.emoji, route.agent)),
            worm_hash: Some(errant.worm_hash.clone()),
            proof_hash: None,
            message:   format!("β†’ {}:{}", route.agent, route.action),
            error:     None,
        }).await;

        // ── Stage 6: NATS publish ─────────────────────────────────────────
        if let Some(nats) = &state.nats {
            let payload = serde_json::json!({
                "action":    req.action,
                "worm_hash": errant.worm_hash,
                "agent":     route.agent,
                "session":   req.session_id,
            });
            match nats.publish(route.subject, &payload).await {
                Ok(_)  => info!("NATS β†’ {}", route.subject),
                Err(e) => error!("NATS publish failed: {e}"),
            }
        }

        send(StageEvent {
            stage: "NATS", ok: true, agent: Some(route.agent.into()),
            worm_hash: Some(errant.worm_hash.clone()),
            proof_hash: None,
            message: format!("published to {}", route.subject), error: None,
        }).await;

        // ── Stage 7: SEAL (Ξ©) ─────────────────────────────────────────────
        let seal_hash = errant_ffi::sha256_hex(
            format!("MAGMA|{}|{}|{}", route.agent, errant.worm_hash, req.action).as_bytes()
        );

        send(StageEvent {
            stage:     "SEAL",
            ok:        true,
            agent:     Some(route.agent.into()),
            worm_hash: Some(seal_hash.clone()),
            proof_hash: Some(seal_hash),
            message:   "Ξ©β†ΊΞ¨β†ΊΞ”β†ΊΞ›β†ΊΞ£β†ΊΞ¦β†ΊΞ±".into(),
            error:     None,
        }).await;
    });

    let stream = ReceiverStream::new(rx).map(|ev| {
        let data = serde_json::to_string(&ev).unwrap_or_default();
        Ok::<Event, Infallible>(Event::default().data(data))
    });

    Sse::new(stream).keep_alive(
        axum::response::sse::KeepAlive::new()
            .interval(Duration::from_secs(15))
            .text("ping"),
    )
}

// ── POST /api/v1/verify ────────────────────────────────────────────────────

async fn verify(
    Json(req): Json<VerifyRequest>,
) -> Json<VerifyResponse> {
    let result = if let Some(ops) = req.opcodes {
        let refs: Vec<&str> = ops.iter().map(|s| s.as_str()).collect();
        errant_ffi::verify_named(&refs)
    } else if let Some(src) = req.source {
        errant_ffi::verify_source(&src)
    } else {
        errant_ffi::verify_named(&[])
    };

    Json(VerifyResponse {
        ok:           result.ok,
        verdict:      result.verdict(),
        worm_hash:    result.worm_hash,
        error:        if result.ok { None } else { Some(result.error) },
        steps:        result.steps,
        lin_consumed: result.lin_consumed,
        lin_leaked:   result.lin_leaked,
        fallback:     result.fallback,
    })
}

// ── GET /api/v1/chain/head ─────────────────────────────────────────────────

async fn chain_head(
    State(state): State<AppState>,
) -> impl IntoResponse {
    // Try to fetch from snap-os; fall back to local WORM hash
    let fallback = errant_ffi::sha256_hex(b"MAGMA-GENESIS");
    Json(serde_json::json!({
        "head":      fallback,
        "source":    "local",
        "os_url":    state.worm_url,
    }))
}

// ── GET /api/v1/health ─────────────────────────────────────────────────────

async fn health(
    State(state): State<AppState>,
) -> impl IntoResponse {
    let errant_ok = errant_ffi::verify_named(&["PUSH_UN", "SEAL"]).ok;
    let nats_ok   = state.nats.is_some();
    let status    = if errant_ok { "ok" } else { "degraded" };
    Json(serde_json::json!({
        "status":     status,
        "version":    env!("CARGO_PKG_VERSION"),
        "errant":     errant_ffi::version(),
        "errant_ok":  errant_ok,
        "nats_ok":    nats_ok,
        "sovereign":  "Ξ©β†ΊΞ¨β†ΊΞ”β†ΊΞ›β†ΊΞ£β†ΊΞ¦β†ΊΞ±",
    }))
}

// ── GET /api/v1/version ────────────────────────────────────────────────────

async fn version_handler() -> impl IntoResponse {
    Json(serde_json::json!({
        "magmad":   env!("CARGO_PKG_VERSION"),
        "liberrant": errant_ffi::version(),
        "protocol": "MAGMA/1.0",
    }))
}

// ── Legacy snap-os compatibility endpoints ─────────────────────────────────

#[derive(Deserialize)]
struct LegacySealPayload { payload: serde_json::Value }

async fn legacy_seal(
    Json(body): Json<LegacySealPayload>,
) -> impl IntoResponse {
    let hash = errant_ffi::sha256_hex(body.payload.to_string().as_bytes());
    Json(serde_json::json!({
        "event": {
            "seal":          hash,
            "previousSeal":  "0".repeat(64),
            "index":         0,
            "timestamp":     chrono::Utc::now().to_rfc3339(),
        }
    }))
}

async fn legacy_dispatch(
    Json(body): Json<serde_json::Value>,
) -> impl IntoResponse {
    let hash = errant_ffi::sha256_hex(body.to_string().as_bytes());
    Json(serde_json::json!({
        "ok":   true,
        "seal": hash,
        "gate": "TRUST-DEED-GATE",
        "sovereign": "Ξ©",
    }))
}

use futures::StreamExt;
use chrono;