bria 0.1.2

Multi-pipeline job orchestrator
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;

use dashmap::DashMap;
use tokio::sync::{broadcast, mpsc};

#[cfg(feature = "server")]
use crate::config;
use crate::context::Job;
use crate::error::Error;

#[cfg(feature = "server")]
use {
    crate::util::{cancel_signal_ttl, prune_expired_cancel_signals},
    axum::extract::ws::{self, WebSocketUpgrade},
    axum::{
        Json, Router,
        body::Bytes,
        extract::{DefaultBodyLimit, OriginalUri, Path, Request, State},
        http::{HeaderMap, StatusCode},
        middleware::{self, Next},
        response::{IntoResponse, Response},
        routing::{delete, get, post},
    },
    hmac::{Hmac, KeyInit, Mac},
    sha2::Sha256,
};

/// Shared server state.
#[derive(Clone)]
pub struct AppState {
    pub config: Arc<crate::config::Config>,
    /// Channels for each source, keyed by source id.
    pub source_txs: Arc<HashMap<String, mpsc::UnboundedSender<Job>>>,
    /// Broadcast channel for stream sink.
    pub broadcast_tx: Option<broadcast::Sender<serde_json::Value>>,
    /// In-memory cancellation signals, keyed by job id.
    pub cancel_signals: Arc<DashMap<String, Instant>>,
    /// Pipeline pause notifiers: one Notify per pipeline id.
    pub pipeline_pauses: Arc<DashMap<String, Arc<tokio::sync::Notify>>>,
}

/// Result of starting the server: the join handle and the shared app state.
pub struct ServerHandle {
    pub join_handle: Option<tokio::task::JoinHandle<()>>,
    pub cancel_signals: Arc<DashMap<String, Instant>>,
    pub pipeline_pauses: Arc<DashMap<String, Arc<tokio::sync::Notify>>>,
}

/// Start the HTTP server if enabled.
pub async fn start_server(
    config: Arc<crate::config::Config>,
    _source_txs: HashMap<String, mpsc::UnboundedSender<Job>>,
    _broadcast_tx: Option<broadcast::Sender<serde_json::Value>>,
    _shutdown_rx: Option<tokio::sync::watch::Receiver<bool>>,
) -> crate::error::Result<ServerHandle> {
    let server_enabled = config.server.enabled;

    if !server_enabled {
        tracing::info!("Server is disabled");
        return Ok(ServerHandle {
            join_handle: None,
            cancel_signals: Arc::new(DashMap::new()),
            pipeline_pauses: Arc::new(DashMap::new()),
        });
    }

    #[cfg(not(feature = "server"))]
    {
        Err(Error::Unsupported(
            "Server requires the 'server' feature".to_string(),
        ))
    }

    #[cfg(feature = "server")]
    {
        start_server_inner(config, _source_txs, _broadcast_tx, _shutdown_rx).await
    }
}

#[cfg(feature = "server")]
async fn start_server_inner(
    config: Arc<crate::config::Config>,
    source_txs: HashMap<String, mpsc::UnboundedSender<Job>>,
    broadcast_tx: Option<broadcast::Sender<serde_json::Value>>,
    shutdown_rx: Option<tokio::sync::watch::Receiver<bool>>,
) -> crate::error::Result<ServerHandle> {
    let server_cfg = &config.server;

    let state = AppState {
        config: config.clone(),
        source_txs: Arc::new(source_txs),
        broadcast_tx,
        cancel_signals: Arc::new(DashMap::new()),
        pipeline_pauses: Arc::new(DashMap::new()),
    };

    let app = build_router(state.clone(), server_cfg);

    let bind_addr = format!("{}:{}", server_cfg.bind, server_cfg.port);
    tracing::info!("Starting server on {bind_addr}");

    let listener = tokio::net::TcpListener::bind(&bind_addr)
        .await
        .map_err(|e| Error::Server(format!("Cannot bind to {bind_addr}: {e}")))?;

    let cancel_signals_ret = state.cancel_signals.clone();
    let pipeline_pauses_ret = state.pipeline_pauses.clone();

    let handle = tokio::spawn(async move {
        axum::serve(listener, app)
            .with_graceful_shutdown(async move {
                if let Some(mut rx) = shutdown_rx {
                    let _ = rx.changed().await;
                } else {
                    // Wait for Ctrl+C
                    let _ = tokio::signal::ctrl_c().await;
                }
                tracing::info!("Server shutting down gracefully...");
            })
            .await
            .unwrap_or_else(|e| {
                tracing::error!("Server error: {e}");
            });
    });

    Ok(ServerHandle {
        join_handle: Some(handle),
        cancel_signals: cancel_signals_ret,
        pipeline_pauses: pipeline_pauses_ret,
    })
}

#[cfg(feature = "server")]
/// Build the Axum router.
fn build_router(state: AppState, server_cfg: &config::ServerConfig) -> Router {
    let prefix = server_cfg.prefix.clone();
    let api_key = server_cfg.api_key.clone();

    let mut router = Router::new()
        // Health check
        .route(&format!("/{prefix}/ping"), get(ping_handler))
        // Pipeline resume (for failure action "stop")
        .route(
            &format!("/{prefix}/pipelines/{{pipeline_id}}/resume"),
            post(resume_pipeline_handler),
        );

    // Add explicit HTTP/webhook source routes. Axum rejects overlapping catch-all routes,
    // so each configured source path gets concrete submit and cancel endpoints.
    for source in &state.config.sources {
        if source.r#type == config::SourceType::Http || source.r#type == config::SourceType::Webhook
        {
            let source_path = source.path.to_string_lossy();
            let source_path = source_path.trim_matches('/');
            if !source_path.is_empty() {
                router = router
                    .route(
                        &format!("/{prefix}/{source_path}"),
                        post(submit_job_handler),
                    )
                    .route(
                        &format!("/{prefix}/{source_path}/{{job_id}}"),
                        delete(cancel_job_handler),
                    );
            }
        }
    }

    // Add stream routes if any stream sinks are configured
    for sink in &state.config.sinks {
        if sink.r#type == config::SinkType::Stream && !sink.sse.is_empty() {
            router = router.route(&format!("/{prefix}/{}", sink.sse), get(sse_handler));
        }
    }

    // Add WebSocket route at /{prefix}/{ws_path} — matches any stream sink's websocket path
    // The handler looks up the sink by path to get ws_heartbeat_secs.
    {
        let has_ws = state
            .config
            .sinks
            .iter()
            .any(|s| s.r#type == config::SinkType::Stream && !s.websocket.is_empty());
        if has_ws {
            router = router.route(&format!("/{prefix}/{{ws_path}}"), get(ws_handler));
        }
    }

    // Dashboard static file serving when server.dashboard is non-empty
    if !server_cfg.dashboard.is_empty() {
        let dashboard_dir = server_cfg.dashboard.clone();
        router = router.nest_service(
            &format!("/{prefix}/dashboard"),
            tower_http::services::ServeDir::new(dashboard_dir),
        );
    }

    // Auth middleware: enforce on ALL routes (ping, source, SSE, WS) when api_key is non-empty
    if !api_key.is_empty() {
        router = router.layer(middleware::from_fn_with_state(
            state.clone(),
            auth_middleware,
        ));
    }

    // Global body size limit (generous server-wide cap; per-source limits checked in handler)
    router = router.layer(DefaultBodyLimit::max(server_cfg.max_body_bytes));

    // CORS (outermost, so preflight OPTIONS bypass auth)
    let cors = tower_http::cors::CorsLayer::permissive();
    router = router.layer(cors);

    router.with_state(state)
}

// ─────────────────────────────────────────────────────────────────────────────
// Auth middleware
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(feature = "server")]
/// Enforce API key authentication on all routes when configured.
async fn auth_middleware(State(state): State<AppState>, request: Request, next: Next) -> Response {
    let api_key = &state.config.server.api_key;
    if api_key.is_empty() {
        return next.run(request).await;
    }

    let headers = request.headers();

    // Check X-Bria-Api-Key header
    if let Some(key) = headers.get("x-bria-api-key").and_then(|v| v.to_str().ok())
        && constant_time_eq(key.trim().as_bytes(), api_key.as_bytes())
    {
        return next.run(request).await;
    }

    // Check Authorization: Bearer <key>
    if let Some(auth) = headers.get("authorization").and_then(|v| v.to_str().ok())
        && let Some(token) = auth.strip_prefix("Bearer ")
        && constant_time_eq(token.trim().as_bytes(), api_key.as_bytes())
    {
        return next.run(request).await;
    }

    (
        StatusCode::UNAUTHORIZED,
        "Unauthorized: invalid or missing API key",
    )
        .into_response()
}

// ─────────────────────────────────────────────────────────────────────────────
// Handlers
// ─────────────────────────────────────────────────────────────────────────────

/// GET /{prefix}/ping — health check.
#[cfg(feature = "server")]
async fn ping_handler() -> &'static str {
    "pong"
}

/// POST /{prefix}/{source_path} — submit a job to an HTTP/webhook source.
#[cfg(feature = "server")]
async fn submit_job_handler(
    State(state): State<AppState>,
    OriginalUri(uri): OriginalUri,
    headers: HeaderMap,
    body: Bytes,
) -> Result<(StatusCode, Json<serde_json::Value>), (StatusCode, String)> {
    let source_path = source_path_from_uri_path(uri.path(), &state.config.server.prefix)
        .ok_or_else(|| {
            (
                StatusCode::NOT_FOUND,
                format!("No HTTP/webhook source found for path: {}", uri.path()),
            )
        })?;

    // Find the source matching this path
    let source = find_http_source_by_path(&state.config, &source_path).ok_or_else(|| {
        (
            StatusCode::NOT_FOUND,
            format!("No HTTP/webhook source found for path: {source_path}"),
        )
    })?;

    // ── HMAC verification for webhook sources ────────────────────────────
    if source.r#type == config::SourceType::Webhook && !source.hmac_secret.is_empty() {
        let sig_header = if source.hmac_header.is_empty() {
            "X-Bria-Signature"
        } else {
            &source.hmac_header
        };

        let raw_sig = headers
            .get(sig_header)
            .and_then(|v| v.to_str().ok())
            .ok_or_else(|| {
                (
                    StatusCode::UNAUTHORIZED,
                    format!("Missing HMAC signature header: {sig_header}"),
                )
            })?;

        // Accept either raw hex or "sha256=" prefix (e.g. GitHub-style)
        let raw_sig = raw_sig.trim();
        let expected_hex = raw_sig.strip_prefix("sha256=").unwrap_or(raw_sig);

        let mut mac =
            Hmac::<Sha256>::new_from_slice(source.hmac_secret.as_bytes()).map_err(|_| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "HMAC key error".to_string(),
                )
            })?;

        mac.update(&body);

        // Constant-time comparison of hex-encoded HMAC
        let computed_hex = hex::encode(mac.finalize().into_bytes());
        if !constant_time_eq(computed_hex.as_bytes(), expected_hex.as_bytes()) {
            return Err((
                StatusCode::UNAUTHORIZED,
                "HMAC signature mismatch".to_string(),
            ));
        }
    }

    // Check max body size against source-specific limit BEFORE JSON parsing
    let max_bytes = if source.max_body_bytes > 0 {
        source.max_body_bytes
    } else {
        state.config.global.max_payload_bytes
    };

    if body.len() > max_bytes {
        return Err((
            StatusCode::PAYLOAD_TOO_LARGE,
            format!(
                "Payload exceeds max_body_bytes limit of {max_bytes} bytes (received {} bytes)",
                body.len()
            ),
        ));
    }

    // Parse JSON
    let payload: serde_json::Value = serde_json::from_slice(&body)
        .map_err(|e| (StatusCode::BAD_REQUEST, format!("Invalid JSON: {e}")))?;

    // Create job
    let job_id = if source.id_field.is_empty() {
        ulid::Ulid::new().to_string()
    } else {
        payload
            .get(&source.id_field)
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
            .unwrap_or_else(|| ulid::Ulid::new().to_string())
    };

    let job = Job {
        id: job_id.clone(),
        source: source.id.clone(),
        payload,
        correlation_key: None,
        labels: source.labels.clone(),
    };

    // Send to source channel
    let tx = state.source_txs.get(&source.id).ok_or_else(|| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            "Source channel not found".to_string(),
        )
    })?;

    tx.send(job).map_err(|e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Failed to submit job: {e}"),
        )
    })?;

    // Determine response status: webhook returns ack_status, HTTP returns 201 Created
    let status = if source.r#type == config::SourceType::Webhook {
        StatusCode::from_u16(source.ack_status).unwrap_or(StatusCode::ACCEPTED)
    } else {
        StatusCode::CREATED
    };

    Ok((
        status,
        Json(serde_json::json!({
            "status": "accepted",
            "job_id": job_id,
        })),
    ))
}

/// DELETE /{prefix}/{source_path}/{id} — cancel a job.
/// Bria is stateless, so we record a cancellation signal and acknowledge the request.
#[cfg(feature = "server")]
async fn cancel_job_handler(
    State(state): State<AppState>,
    OriginalUri(uri): OriginalUri,
    Path(job_id): Path<String>,
) -> Result<(StatusCode, Json<serde_json::Value>), (StatusCode, String)> {
    let cancel_path = source_path_from_uri_path(uri.path(), &state.config.server.prefix)
        .ok_or_else(|| {
            (
                StatusCode::BAD_REQUEST,
                format!(
                    "Invalid cancel path: expected {{source_path}}/{{id}}, got '{}'",
                    uri.path()
                ),
            )
        })?;
    let source_path = cancel_path
        .strip_suffix(&format!("/{job_id}"))
        .ok_or_else(|| {
            (
                StatusCode::BAD_REQUEST,
                format!(
                    "Invalid cancel path: expected {{source_path}}/{{id}}, got '{cancel_path}'"
                ),
            )
        })?;
    if source_path.is_empty() || job_id.is_empty() {
        return Err((
            StatusCode::BAD_REQUEST,
            format!("Invalid cancel path: expected {{source_path}}/{{id}}, got '{cancel_path}'"),
        ));
    }

    // Verify the source exists
    let source = find_http_source_by_path(&state.config, source_path).ok_or_else(|| {
        (
            StatusCode::NOT_FOUND,
            format!("No HTTP/webhook source found for path: {source_path}"),
        )
    })?;

    // Record cancellation signal
    prune_expired_cancel_signals(&state.cancel_signals, cancel_signal_ttl(&state.config));
    state.cancel_signals.insert(job_id.clone(), Instant::now());

    tracing::info!(
        "Cancellation requested for job '{}' on source '{}' (path: {source_path})",
        job_id,
        source.id
    );

    // Publish cancellation event to broadcast channel if available
    if let Some(ref tx) = state.broadcast_tx {
        let msg = serde_json::json!({
            "type": "cancellation_requested",
            "job_id": job_id,
            "source_id": source.id,
            "source_path": source_path,
        });
        let _ = tx.send(msg);
    }

    // Also try to send a cancellation message to the source channel
    // (the pipeline worker can check cancel_signals to skip processing)
    if let Some(tx) = state.source_txs.get(&source.id) {
        // Construct a synthetic cancellation job with a magic marker
        let cancel_job = Job {
            id: format!("__cancel__{}", job_id),
            source: source.id.clone(),
            payload: serde_json::json!({
                "__bria_cancel": true,
                "target_job_id": job_id,
            }),
            correlation_key: None,
            labels: source.labels.clone(),
        };
        let _ = tx.send(cancel_job);
    }

    Ok((
        StatusCode::ACCEPTED,
        Json(serde_json::json!({
            "status": "cancellation_requested",
            "job_id": job_id,
        })),
    ))
}

// ─────────────────────────────────────────────────────────────────────────────
// SSE / WebSocket handlers
// ─────────────────────────────────────────────────────────────────────────────

/// GET /{prefix}/sse — Server-Sent Events stream of results.
#[cfg(feature = "server")]
async fn sse_handler(
    State(state): State<AppState>,
) -> axum::response::Sse<
    impl tokio_stream::Stream<
        Item = std::result::Result<axum::response::sse::Event, std::convert::Infallible>,
    >,
> {
    use std::pin::Pin;

    let stream: Pin<
        Box<
            dyn tokio_stream::Stream<
                    Item = std::result::Result<
                        axum::response::sse::Event,
                        std::convert::Infallible,
                    >,
                > + Send,
        >,
    > = if let Some(ref tx) = state.broadcast_tx {
        let mut rx = tx.subscribe();
        Box::pin(async_stream::stream! {
            loop {
                match rx.recv().await {
                    Ok(value) => {
                        let data = value.to_string();
                        yield Ok(axum::response::sse::Event::default().data(data));
                    }
                    Err(broadcast::error::RecvError::Lagged(_)) => {
                        continue;
                    }
                    Err(broadcast::error::RecvError::Closed) => {
                        break;
                    }
                }
            }
        })
    } else {
        Box::pin(async_stream::stream! {
            yield Ok(axum::response::sse::Event::default().data("{\"error\":\"stream not configured\"}"));
        })
    };

    let keepalive_secs = state
        .config
        .sinks
        .iter()
        .find(|s| s.r#type == config::SinkType::Stream && !s.sse.is_empty())
        .map(|s| s.sse_keepalive_secs.max(1))
        .unwrap_or(5);

    axum::response::Sse::new(stream).keep_alive(
        axum::response::sse::KeepAlive::new()
            .interval(std::time::Duration::from_secs(keepalive_secs))
            .text("keepalive"),
    )
}

/// GET /{prefix}/ws — WebSocket stream of results.
#[cfg(feature = "server")]
async fn ws_handler(
    State(state): State<AppState>,
    Path(ws_path): Path<String>,
    ws: WebSocketUpgrade,
) -> Response {
    let heartbeat = state
        .config
        .sinks
        .iter()
        .find(|s| s.r#type == config::SinkType::Stream && s.websocket == ws_path)
        .map(|s| s.ws_heartbeat_secs)
        .unwrap_or(30);

    let broadcast_tx = state.broadcast_tx.clone();
    ws.on_upgrade(move |socket| handle_ws_socket(socket, broadcast_tx, heartbeat))
}

/// Handle an upgraded WebSocket connection: send broadcast events as text and
/// periodic ping frames. Close gracefully when the broadcast channel is gone.
#[cfg(feature = "server")]
async fn handle_ws_socket(
    mut socket: ws::WebSocket,
    broadcast_tx: Option<broadcast::Sender<serde_json::Value>>,
    heartbeat_secs: u64,
) {
    let Some(tx) = broadcast_tx else {
        let frame = ws::CloseFrame {
            code: ws::close_code::NORMAL,
            reason: ws::Utf8Bytes::from_static("stream not configured: no broadcast channel"),
        };
        let _ = socket.send(ws::Message::Close(Some(frame))).await;
        return;
    };

    let mut rx = tx.subscribe();
    let mut heartbeat = tokio::time::interval(std::time::Duration::from_secs(heartbeat_secs));

    loop {
        tokio::select! {
            result = rx.recv() => {
                match result {
                    Ok(value) => {
                        let text = value.to_string();
                        if socket.send(ws::Message::Text(ws::Utf8Bytes::from(text))).await.is_err() {
                            break;
                        }
                    }
                    Err(broadcast::error::RecvError::Lagged(_)) => {
                        continue;
                    }
                    Err(broadcast::error::RecvError::Closed) => {
                        let frame = ws::CloseFrame {
                            code: ws::close_code::NORMAL,
                            reason: ws::Utf8Bytes::from_static("stream ended"),
                        };
                        let _ = socket.send(ws::Message::Close(Some(frame))).await;
                        break;
                    }
                }
            }
            _ = heartbeat.tick() => {
                // Use an empty ping frame
                let empty: Vec<u8> = Vec::new();
                if socket.send(ws::Message::Ping(empty.into())).await.is_err() {
                    break;
                }
            }
            msg = socket.recv() => {
                match msg {
                    Some(Ok(ws::Message::Close(_))) | None => break,
                    Some(Ok(_)) => {} // ignore other client messages
                    Some(Err(_)) => break,
                }
            }
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────────────────────

/// Find an HTTP or Webhook source by its URL path component.
#[cfg(feature = "server")]
fn find_http_source_by_path<'a>(
    config: &'a crate::config::Config,
    path: &str,
) -> Option<&'a config::SourceConfig> {
    config.sources.iter().find(|s| {
        (s.r#type == config::SourceType::Http || s.r#type == config::SourceType::Webhook)
            && s.path.to_string_lossy() == path
    })
}

#[cfg(feature = "server")]
fn source_path_from_uri_path(path: &str, prefix: &str) -> Option<String> {
    let prefix = format!("/{}", prefix.trim_matches('/'));
    path.strip_prefix(&prefix)
        .and_then(|path| path.strip_prefix('/'))
        .filter(|path| !path.is_empty())
        .map(ToOwned::to_owned)
}

/// POST /{prefix}/pipelines/{pipeline_id}/resume — resume a stopped pipeline.
#[cfg(feature = "server")]
async fn resume_pipeline_handler(
    State(state): State<AppState>,
    Path(pipeline_id): Path<String>,
) -> Result<(StatusCode, Json<serde_json::Value>), (StatusCode, String)> {
    // Verify the pipeline exists
    let pipeline_exists = state.config.pipelines.iter().any(|p| p.id == pipeline_id);
    if !pipeline_exists {
        return Err((
            StatusCode::NOT_FOUND,
            format!("Pipeline '{pipeline_id}' not found"),
        ));
    }

    // Get or create the notifier and notify
    let notify = state
        .pipeline_pauses
        .entry(pipeline_id.clone())
        .or_insert_with(|| Arc::new(tokio::sync::Notify::new()))
        .clone();
    notify.notify_waiters();

    tracing::info!("Pipeline '{}' resumed by operator", pipeline_id);

    Ok((
        StatusCode::OK,
        Json(serde_json::json!({
            "status": "resumed",
            "pipeline_id": pipeline_id,
        })),
    ))
}

/// Constant-time byte comparison to avoid timing side-channels on HMAC.
#[cfg(feature = "server")]
fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }
    let mut diff = 0u8;
    for (x, y) in a.iter().zip(b.iter()) {
        diff |= x ^ y;
    }
    diff == 0
}