nvsim-server 0.3.1

Axum REST + WebSocket server fronting the nvsim NV-diamond pipeline simulator (ADR-092 §6.2).
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
416
417
418
419
420
//! `nvsim-server` — Axum host fronting the deterministic nvsim pipeline.
//!
//! ADR-092 §6.2 — REST control plane + binary WebSocket data plane.
//! Same `(scene, config, seed)` produces byte-identical witnesses across
//! the WASM transport (in-browser worker) and this WS transport — the
//! determinism contract the dashboard's Verify panel asserts.
//!
//! ## Routes
//!
//! | Method | Path                    | Purpose                          |
//! |--------|-------------------------|----------------------------------|
//! | GET    | /api/health             | liveness + nvsim version + magic |
//! | GET    | /api/scene              | current scene (JSON)             |
//! | PUT    | /api/scene              | replace scene                    |
//! | GET    | /api/config             | current `PipelineConfig`         |
//! | PUT    | /api/config             | replace config                   |
//! | GET    | /api/seed               | current seed (hex)               |
//! | PUT    | /api/seed               | set seed                         |
//! | POST   | /api/run                | start a run                      |
//! | POST   | /api/pause              | pause                            |
//! | POST   | /api/reset              | reset to t=0                     |
//! | POST   | /api/step               | single step                      |
//! | POST   | /api/witness/generate   | run N frames + return SHA-256    |
//! | POST   | /api/witness/verify     | re-derive + compare expected     |
//! | POST   | /api/witness/reference  | run canonical Proof::generate    |
//! | POST   | /api/export-proof       | proof bundle as JSON             |
//! | GET    | /ws/stream              | binary MagFrame batch stream     |

use std::net::SocketAddr;
use std::sync::Arc;

use axum::{
    extract::{
        ws::{Message, WebSocket, WebSocketUpgrade},
        State,
    },
    http::StatusCode,
    response::IntoResponse,
    routing::{get, post},
    Json, Router,
};
use clap::Parser;
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;
use tower_http::{
    cors::{Any, CorsLayer},
    trace::TraceLayer,
};
use tracing::{info, warn};

use nvsim::{
    pipeline::{Pipeline, PipelineConfig},
    proof::Proof,
    scene::Scene,
};

#[derive(Parser, Debug)]
#[command(name = "nvsim-server", version)]
struct Args {
    #[arg(long, default_value = "127.0.0.1:7878")]
    listen: SocketAddr,
    #[arg(long, default_value = "*")]
    allowed_origin: String,
}

#[derive(Debug, Clone)]
struct AppState {
    inner: Arc<Mutex<RunState>>,
}

#[derive(Debug, Clone)]
struct RunState {
    scene: Scene,
    config: PipelineConfig,
    seed: u64,
    running: bool,
    frames_emitted: u64,
}

impl AppState {
    fn new() -> Self {
        let scene = Proof::reference_scene().expect("reference scene parses");
        Self {
            inner: Arc::new(Mutex::new(RunState {
                scene,
                config: PipelineConfig::default(),
                seed: Proof::SEED,
                running: false,
                frames_emitted: 0,
            })),
        }
    }
}

#[derive(Serialize)]
struct HealthBody {
    nvsim_version: &'static str,
    magic: u32,
    frame_bytes: usize,
    expected_witness_hex: &'static str,
}

#[derive(Serialize)]
struct SeedBody {
    seed_hex: String,
}

#[derive(Deserialize)]
struct SeedReq {
    seed_hex: String,
}

#[derive(Deserialize, Default)]
struct WitnessReq {
    samples: Option<usize>,
}

#[derive(Serialize)]
struct WitnessBody {
    witness_hex: String,
    samples: usize,
    seed_hex: String,
}

#[derive(Deserialize)]
struct VerifyReq {
    expected_hex: String,
    samples: Option<usize>,
}

#[derive(Serialize)]
struct VerifyBody {
    ok: bool,
    actual_hex: String,
    expected_hex: String,
}

/// Incoming request body for the `/step` endpoint.
/// Fields are optional; unused ones are reserved for future extensions.
#[derive(Deserialize)]
#[allow(dead_code)]
struct StepReq {
    direction: Option<String>,
    dt_ms: Option<f64>,
}

#[derive(Serialize)]
struct ProofBundle {
    kind: &'static str,
    nvsim_version: &'static str,
    seed_hex: String,
    n_samples: usize,
    witness_hex: String,
    expected_hex: &'static str,
    ok: bool,
    ts: String,
}

const EXPECTED_WITNESS_HEX: &str =
    "cc8de9b01b0ff5bd97a6c17848a3f156c174ea7589d0888164a441584ec593b4";

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| "nvsim_server=info,tower_http=info".into()),
        )
        .init();

    let args = Args::parse();
    let state = AppState::new();
    let cors = CorsLayer::new()
        .allow_origin(if args.allowed_origin == "*" {
            tower_http::cors::AllowOrigin::any()
        } else {
            args.allowed_origin
                .parse::<axum::http::HeaderValue>()
                .map(tower_http::cors::AllowOrigin::exact)
                .unwrap_or_else(|_| tower_http::cors::AllowOrigin::any())
        })
        .allow_headers(Any)
        .allow_methods(Any);

    let app = Router::new()
        .route("/api/health", get(health))
        .route("/api/scene", get(get_scene).put(put_scene))
        .route("/api/config", get(get_config).put(put_config))
        .route("/api/seed", get(get_seed).put(put_seed))
        .route("/api/run", post(run_pipe))
        .route("/api/pause", post(pause_pipe))
        .route("/api/reset", post(reset_pipe))
        .route("/api/step", post(step_pipe))
        .route("/api/witness/generate", post(witness_generate))
        .route("/api/witness/verify", post(witness_verify))
        .route("/api/witness/reference", post(witness_reference))
        .route("/api/export-proof", post(export_proof))
        .route("/ws/stream", get(ws_handler))
        .with_state(state)
        .layer(cors)
        .layer(TraceLayer::new_for_http());

    info!("nvsim-server listening on http://{}", args.listen);
    let listener = tokio::net::TcpListener::bind(args.listen)
        .await
        .expect("bind listener");
    axum::serve(listener, app).await.expect("axum serve");
}

async fn health() -> Json<HealthBody> {
    Json(HealthBody {
        nvsim_version: env!("CARGO_PKG_VERSION"),
        magic: nvsim::MAG_FRAME_MAGIC,
        frame_bytes: nvsim::frame::MAG_FRAME_BYTES,
        expected_witness_hex: EXPECTED_WITNESS_HEX,
    })
}

async fn get_scene(State(s): State<AppState>) -> Json<Scene> {
    Json(s.inner.lock().await.scene.clone())
}

async fn put_scene(
    State(s): State<AppState>,
    Json(scene): Json<Scene>,
) -> Result<&'static str, AppError> {
    s.inner.lock().await.scene = scene;
    Ok("ok")
}

async fn get_config(State(s): State<AppState>) -> Json<PipelineConfig> {
    Json(s.inner.lock().await.config)
}

async fn put_config(
    State(s): State<AppState>,
    Json(cfg): Json<PipelineConfig>,
) -> Result<&'static str, AppError> {
    s.inner.lock().await.config = cfg;
    Ok("ok")
}

async fn get_seed(State(s): State<AppState>) -> Json<SeedBody> {
    let seed = s.inner.lock().await.seed;
    Json(SeedBody {
        seed_hex: format!("0x{:016X}", seed),
    })
}

async fn put_seed(
    State(s): State<AppState>,
    Json(req): Json<SeedReq>,
) -> Result<&'static str, AppError> {
    let raw = req.seed_hex.trim().trim_start_matches("0x");
    let seed = u64::from_str_radix(raw, 16).map_err(|e| AppError::BadInput(e.to_string()))?;
    s.inner.lock().await.seed = seed;
    Ok("ok")
}

async fn run_pipe(State(s): State<AppState>) -> &'static str {
    s.inner.lock().await.running = true;
    "running"
}

async fn pause_pipe(State(s): State<AppState>) -> &'static str {
    s.inner.lock().await.running = false;
    "paused"
}

async fn reset_pipe(State(s): State<AppState>) -> &'static str {
    let mut g = s.inner.lock().await;
    g.frames_emitted = 0;
    g.running = false;
    "reset"
}

async fn step_pipe(
    State(s): State<AppState>,
    Json(_req): Json<StepReq>,
) -> Result<&'static str, AppError> {
    s.inner.lock().await.frames_emitted += 1;
    Ok("ok")
}

async fn witness_generate(
    State(s): State<AppState>,
    Json(req): Json<WitnessReq>,
) -> Json<WitnessBody> {
    let n = req.samples.unwrap_or(256);
    let g = s.inner.lock().await;
    let pipeline = Pipeline::new(g.scene.clone(), g.config, g.seed);
    let (_, witness) = pipeline.run_with_witness(n);
    Json(WitnessBody {
        witness_hex: Proof::hex(&witness),
        samples: n,
        seed_hex: format!("0x{:016X}", g.seed),
    })
}

async fn witness_verify(
    State(_s): State<AppState>,
    Json(req): Json<VerifyReq>,
) -> Result<Json<VerifyBody>, AppError> {
    // ADR-092 §6.3 — verify always runs the *canonical* reference scene
    // (Proof::generate) so it matches Proof::EXPECTED_WITNESS_HEX. The
    // user's working scene/config/seed don't enter this check.
    let _samples = req.samples.unwrap_or(Proof::N_SAMPLES);
    let actual = Proof::generate().map_err(|e| AppError::Internal(e.to_string()))?;
    let actual_hex = Proof::hex(&actual);
    let expected_hex = req.expected_hex.trim().to_lowercase();
    let ok = actual_hex == expected_hex;
    Ok(Json(VerifyBody {
        ok,
        actual_hex,
        expected_hex,
    }))
}

async fn witness_reference() -> Result<Json<WitnessBody>, AppError> {
    let actual = Proof::generate().map_err(|e| AppError::Internal(e.to_string()))?;
    Ok(Json(WitnessBody {
        witness_hex: Proof::hex(&actual),
        samples: Proof::N_SAMPLES,
        seed_hex: format!("0x{:016X}", Proof::SEED),
    }))
}

async fn export_proof(State(_s): State<AppState>) -> Result<Json<ProofBundle>, AppError> {
    let actual = Proof::generate().map_err(|e| AppError::Internal(e.to_string()))?;
    let actual_hex = Proof::hex(&actual);
    let ok = actual_hex == EXPECTED_WITNESS_HEX;
    Ok(Json(ProofBundle {
        kind: "nvsim-proof-bundle",
        nvsim_version: env!("CARGO_PKG_VERSION"),
        seed_hex: format!("0x{:016X}", Proof::SEED),
        n_samples: Proof::N_SAMPLES,
        witness_hex: actual_hex,
        expected_hex: EXPECTED_WITNESS_HEX,
        ok,
        ts: chrono_like_now(),
    }))
}

fn chrono_like_now() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);
    format!("{secs}-unix")
}

async fn ws_handler(ws: WebSocketUpgrade, State(s): State<AppState>) -> impl IntoResponse {
    ws.on_upgrade(move |socket| handle_ws(socket, s))
}

async fn handle_ws(mut socket: WebSocket, state: AppState) {
    info!("ws/stream client connected");
    // Build the pipeline on connect — single instance per client; the
    // server doesn't multiplex pipelines because the sim is fast enough
    // to spin one up per client without measurable latency.
    let (scene, config, seed) = {
        let g = state.inner.lock().await;
        (g.scene.clone(), g.config, g.seed)
    };
    let pipeline = Pipeline::new(scene, config, seed);
    let mut tick = tokio::time::interval(std::time::Duration::from_millis(16));
    let batch_size = 32usize;

    loop {
        tokio::select! {
            _ = tick.tick() => {
                let running = { state.inner.lock().await.running };
                if !running { continue; }

                let frames = pipeline.run(batch_size);
                let mut bytes = Vec::with_capacity(frames.len() * nvsim::frame::MAG_FRAME_BYTES);
                for f in &frames { bytes.extend_from_slice(&f.to_bytes()); }
                if socket.send(Message::Binary(bytes)).await.is_err() {
                    warn!("ws/stream client closed");
                    return;
                }

                let mut g = state.inner.lock().await;
                g.frames_emitted = g.frames_emitted.saturating_add(frames.len() as u64);
            }
            msg = socket.recv() => {
                match msg {
                    Some(Ok(Message::Close(_))) | None => {
                        info!("ws/stream client disconnected");
                        return;
                    }
                    Some(Ok(_)) => { /* ignore inbound messages in V1 */ }
                    Some(Err(e)) => {
                        warn!(?e, "ws/stream socket error");
                        return;
                    }
                }
            }
        }
    }
}

#[derive(Debug, thiserror::Error)]
enum AppError {
    #[error("bad input: {0}")]
    BadInput(String),
    #[error("internal: {0}")]
    Internal(String),
}

impl IntoResponse for AppError {
    fn into_response(self) -> axum::response::Response {
        let (code, msg) = match &self {
            AppError::BadInput(_) => (StatusCode::BAD_REQUEST, self.to_string()),
            AppError::Internal(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
        };
        (code, msg).into_response()
    }
}