trace-weft-server 0.2.0

Axum API and query layer for TraceWeft (SQLite, Postgres, auth, OTLP ingest)
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
pub mod auth;
pub mod storage;

use auth::{Auth, AuthConfig};
use axum::{
    Json, Router,
    extract::{Path, State},
    http::{HeaderMap, HeaderValue, Method, StatusCode, header},
    routing::{get, post},
};
use sqlx::{PgPool, Row, SqlitePool, postgres::PgPoolOptions, sqlite::SqlitePoolOptions};
use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::Arc;
use tower_http::cors::{AllowOrigin, CorsLayer};
use trace_weft_core::SpanRecord;
use trace_weft_recorder::TraceStore;

#[derive(Clone)]
pub enum DbPool {
    Sqlite(SqlitePool),
    Postgres(PgPool),
}

#[derive(Clone)]
pub struct AppState {
    pub pool: DbPool,
    pub blob_store: Arc<dyn trace_weft_core::BlobStore>,
    pub trace_store: Arc<dyn TraceStore>,
    pub clickhouse: Option<Arc<storage::analytics::ClickHouseAnalytics>>,
    pub auth: Arc<AuthConfig>,
}

/// Start the server with the **production-secure** auth default
/// ([`AuthConfig::from_env`]): unauthenticated requests are rejected unless
/// `TRACE_WEFT_API_KEYS`/`TRACE_WEFT_DEV_MODE` are configured. Runs until the
/// process ends.
pub async fn start_server(db_url: &str, port: u16, blob_dir: PathBuf) -> anyhow::Result<()> {
    start_server_with_shutdown(
        db_url,
        port,
        blob_dir,
        AuthConfig::from_env(),
        std::future::pending::<()>(),
    )
    .await
}

/// Start a **local-first** dev server: the auth bypass defaults on when no keys
/// are configured (see [`AuthConfig::from_env_local_first`]), so the local UI
/// works without keys. Used by `trace-weft dev`.
pub async fn start_dev_server(db_url: &str, port: u16, blob_dir: PathBuf) -> anyhow::Result<()> {
    start_server_with_shutdown(
        db_url,
        port,
        blob_dir,
        AuthConfig::from_env_local_first(),
        std::future::pending::<()>(),
    )
    .await
}

/// Start the server with an explicit [`AuthConfig`], stopping gracefully when
/// `shutdown` resolves. Used by the desktop app to start/stop the embedded
/// server on demand and to drain it cleanly on app exit.
pub async fn start_server_with_shutdown(
    db_url: &str,
    port: u16,
    blob_dir: PathBuf,
    auth: AuthConfig,
    shutdown: impl std::future::Future<Output = ()> + Send + 'static,
) -> anyhow::Result<()> {
    let pool = if db_url.starts_with("postgres://") || db_url.starts_with("postgresql://") {
        let pg_pool = PgPoolOptions::new().connect(db_url).await?;
        DbPool::Postgres(pg_pool)
    } else {
        // Assume sqlite file path or sqlite:// url
        let url = if db_url.starts_with("sqlite://") {
            db_url.to_string()
        } else {
            if let Some(parent) = std::path::Path::new(db_url).parent() {
                tokio::fs::create_dir_all(parent).await?;
            }
            format!("sqlite://{}?mode=rwc", db_url)
        };
        let sq_pool = SqlitePoolOptions::new().connect(&url).await?;
        DbPool::Sqlite(sq_pool)
    };

    let blob_store = Arc::new(storage::blob::LocalBlobStore::new(blob_dir));

    let trace_store: Arc<dyn TraceStore> = match &pool {
        DbPool::Postgres(pg_pool) => Arc::new(storage::postgres::PostgresRecorder {
            pool: pg_pool.clone(),
        }),
        DbPool::Sqlite(sq_pool) => {
            Arc::new(trace_weft_recorder::sqlite::SqliteRecorder::from_pool(sq_pool.clone()).await?)
        }
    };

    // Enterprise Analytics (Stubbed connection if env var is present)
    let clickhouse = if let Ok(ch_url) = std::env::var("TRACE_WEFT_CH_URL") {
        tracing::info!("Initializing ClickHouse analytics connected to {}", ch_url);
        Some(Arc::new(storage::analytics::ClickHouseAnalytics::new(
            &ch_url, "default", "", "default",
        )))
    } else {
        None
    };

    let state = AppState {
        pool,
        blob_store,
        trace_store,
        clickhouse,
        auth: Arc::new(auth),
    };

    let app = build_router(state);

    let addr = SocketAddr::from(([127, 0, 0, 1], port));
    tracing::info!("Server listening on http://{}", addr);

    let listener = tokio::net::TcpListener::bind(addr).await?;
    axum::serve(listener, app)
        .with_graceful_shutdown(shutdown)
        .await?;

    Ok(())
}

/// Build the TraceWeft API router over the given application state.
pub fn build_router(state: AppState) -> Router {
    Router::new()
        .route("/api/traces", get(list_traces))
        .route("/api/traces/{trace_id}", get(get_trace))
        .route("/api/evals", get(list_evals))
        .route("/api/v1/batch", post(batch_ingest))
        .route("/api/hitl/pending", get(get_pending_approvals))
        .route("/api/hitl/resolve", post(resolve_approval))
        .layer(local_cors())
        .with_state(state)
}

/// CORS for a local-first server: only the local dev UI and the desktop webview
/// may read API responses. A permissive policy would let any website the user
/// visits script `127.0.0.1:<port>` and exfiltrate locally-stored prompts and
/// tool outputs (and, for JSON `POST`s, drive HITL/ingest via CSRF). Restricting
/// the allowed origins makes the browser block both.
fn local_cors() -> CorsLayer {
    CorsLayer::new()
        .allow_methods([Method::GET, Method::POST, Method::OPTIONS])
        .allow_headers([header::AUTHORIZATION, header::CONTENT_TYPE])
        .allow_origin(AllowOrigin::predicate(|origin: &HeaderValue, _req| {
            origin.to_str().map(is_allowed_origin).unwrap_or(false)
        }))
}

/// Allow the Tauri webview origins and loopback (any port, for the Vite dev
/// server and direct browser access); reject everything else.
fn is_allowed_origin(origin: &str) -> bool {
    if origin == "tauri://localhost" || origin == "http://tauri.localhost" {
        return true;
    }
    ["http://localhost", "http://127.0.0.1"]
        .iter()
        .any(|host| origin == *host || origin.starts_with(&format!("{host}:")))
}

/// Resolve the request's API key to a tenant, or `401` when none is valid and
/// the dev bypass is off.
fn authorize(state: &AppState, headers: &HeaderMap) -> Result<Auth, StatusCode> {
    state
        .auth
        .authenticate(headers)
        .ok_or(StatusCode::UNAUTHORIZED)
}

async fn batch_ingest(
    headers: HeaderMap,
    State(state): State<AppState>,
    Json(mut spans): Json<Vec<SpanRecord>>,
) -> Result<StatusCode, StatusCode> {
    let auth = authorize(&state, &headers)?;
    // The server is authoritative on tenancy: stamp the authenticated project
    // onto every span so a client cannot assert someone else's project_id.
    let project_id = auth.project().map(|p| p.to_string());
    for span in &mut spans {
        span.project_id = project_id.clone();
    }

    tracing::info!(
        "Received batch of {} spans for project {:?}",
        spans.len(),
        project_id
    );

    // 1. Ingest metadata into Postgres
    for span in &spans {
        // In a real app, this should be a bulk insert
        if let Err(e) = state.trace_store.record_span(span.clone()).await {
            tracing::error!("Failed to record span: {}", e);
            return Err(StatusCode::INTERNAL_SERVER_ERROR);
        }
    }

    // 2. Stream to ClickHouse for analytics
    if let Some(ch) = &state.clickhouse
        && let Err(e) = ch.ingest_batch(&spans).await
    {
        tracing::warn!("Failed to stream to ClickHouse: {}", e);
    }

    Ok(StatusCode::ACCEPTED)
}

/// Log a database error and surface it as a 500. Used by every query handler so
/// failures are recorded rather than silently flattened to an empty body.
fn db_error<E: std::fmt::Display>(e: E) -> StatusCode {
    tracing::error!("database query failed: {e}");
    StatusCode::INTERNAL_SERVER_ERROR
}

/// Decode a JSON column we wrote ourselves. A parse failure means the row is
/// corrupt, so we surface a 500 instead of masking it with an empty object —
/// silently substituting `{}` would hide data loss from the caller.
fn parse_json_column(raw: &str) -> Result<serde_json::Value, StatusCode> {
    serde_json::from_str(raw).map_err(|e| {
        tracing::error!("corrupt JSON in spans column: {e}");
        StatusCode::INTERNAL_SERVER_ERROR
    })
}

/// Decode a nullable JSON column, preserving SQL `NULL` as JSON `null`.
fn parse_opt_json_column(raw: Option<String>) -> Result<serde_json::Value, StatusCode> {
    match raw {
        Some(s) => parse_json_column(&s),
        None => Ok(serde_json::Value::Null),
    }
}

// The SQLite and Postgres `spans` tables share an identical column layout, so a
// single row shape maps to JSON for either backend. These macros expand the
// same extraction against `SqliteRow` or `PgRow` (the `?` inside propagates to
// the calling handler), keeping the two dialects from drifting apart.

/// One row of the trace-summary aggregate (see `list_traces`).
macro_rules! trace_summary_json {
    ($row:expr) => {{
        let row = $row;
        let trace_id: String = row.get("trace_id");
        let run_id: String = row.get("run_id");
        let start_time: i64 = row.get("start_time");
        let end_time: Option<i64> = row.get("end_time");
        let span_count: i64 = row.get("span_count");
        let has_error: i64 = row.get("has_error");
        serde_json::json!({
            "trace_id": trace_id,
            "run_id": run_id,
            "start_time": start_time,
            "end_time": end_time,
            "span_count": span_count,
            // A trace is errored if any of its spans errored, otherwise ok.
            "status": if has_error != 0 { "error" } else { "ok" },
        })
    }};
}

/// One evaluator span row (see `list_evals`).
macro_rules! eval_row_json {
    ($row:expr) => {{
        let row = $row;
        let trace_id: String = row.get("trace_id");
        let span_id: String = row.get("span_id");
        let name: String = row.get("name");
        let start_time: i64 = row.get("start_time");
        let status: String = row.get("status");
        let attributes: String = row.get("attributes");
        serde_json::json!({
            "trace_id": trace_id,
            "span_id": span_id,
            "name": name,
            "start_time": start_time,
            "status": status,
            "attributes": parse_json_column(&attributes)?,
        })
    }};
}

/// One full span row (see `get_trace`).
macro_rules! span_detail_json {
    ($row:expr) => {{
        let row = $row;
        let trace_id: String = row.get("trace_id");
        let span_id: String = row.get("span_id");
        let parent_span_id: Option<String> = row.get("parent_span_id");
        let span_kind: String = row.get("span_kind");
        let name: String = row.get("name");
        let start_time: i64 = row.get("start_time");
        let end_time: Option<i64> = row.get("end_time");
        let status: String = row.get("status");
        let attributes: String = row.get("attributes");
        let latency_ms: Option<i64> = row.get("latency_ms");
        let input_ref: Option<String> = row.get("input_ref");
        let output_ref: Option<String> = row.get("output_ref");
        serde_json::json!({
            "trace_id": trace_id,
            "span_id": span_id,
            "parent_span_id": parent_span_id,
            "span_kind": span_kind,
            "name": name,
            "start_time": start_time,
            "end_time": end_time,
            "status": status,
            "attributes": parse_json_column(&attributes)?,
            "latency_ms": latency_ms,
            "input_ref": parse_opt_json_column(input_ref)?,
            "output_ref": parse_opt_json_column(output_ref)?,
        })
    }};
}

// Project scoping: each query filters on `project_id` against the bound
// `project` value. A real tenant binds its project id; the dev bypass binds
// SQL `NULL`, and the `OR <param> IS NULL` arm then matches every row so
// local-first runs see all traces. Postgres reuses one `$1`; SQLite repeats the
// positional `?`, so the project value is bound twice there.
//
// The aggregate is portable: every span of a trace shares a run_id (so
// MIN(run_id) is deterministic), the error rollup is CAST to BIGINT so both
// engines decode it as i64, and only grouped/aggregated columns are selected so
// Postgres (which rejects bare columns under GROUP BY) is happy.
const LIST_TRACES_SQL_SQLITE: &str = r#"
    SELECT trace_id, MIN(run_id) AS run_id, MIN(start_time) AS start_time,
           MAX(end_time) AS end_time, COUNT(span_id) AS span_count,
           CAST(MAX(CASE WHEN status = 'error' THEN 1 ELSE 0 END) AS BIGINT) AS has_error
    FROM spans
    WHERE (project_id = ? OR ? IS NULL)
    GROUP BY trace_id
    ORDER BY start_time DESC
    LIMIT 50
"#;

const LIST_TRACES_SQL_PG: &str = r#"
    SELECT trace_id, MIN(run_id) AS run_id, MIN(start_time) AS start_time,
           MAX(end_time) AS end_time, COUNT(span_id) AS span_count,
           CAST(MAX(CASE WHEN status = 'error' THEN 1 ELSE 0 END) AS BIGINT) AS has_error
    FROM spans
    WHERE (project_id = $1 OR $1 IS NULL)
    GROUP BY trace_id
    ORDER BY start_time DESC
    LIMIT 50
"#;

const LIST_EVALS_SQL_SQLITE: &str = r#"
    SELECT trace_id, span_id, name, start_time, status, attributes
    FROM spans
    WHERE (span_kind = 'evaluator' OR span_kind = 'Evaluator')
      AND (project_id = ? OR ? IS NULL)
    ORDER BY start_time DESC
    LIMIT 50
"#;

const LIST_EVALS_SQL_PG: &str = r#"
    SELECT trace_id, span_id, name, start_time, status, attributes
    FROM spans
    WHERE (span_kind = 'evaluator' OR span_kind = 'Evaluator')
      AND (project_id = $1 OR $1 IS NULL)
    ORDER BY start_time DESC
    LIMIT 50
"#;

const GET_TRACE_SQL_SQLITE: &str = "SELECT * FROM spans WHERE trace_id = ? AND (project_id = ? OR ? IS NULL) ORDER BY start_time ASC";

const GET_TRACE_SQL_PG: &str = "SELECT * FROM spans WHERE trace_id = $1 AND (project_id = $2 OR $2 IS NULL) ORDER BY start_time ASC";

async fn list_traces(
    headers: HeaderMap,
    State(state): State<AppState>,
) -> Result<Json<Vec<serde_json::Value>>, StatusCode> {
    let project = authorize(&state, &headers)?.project().map(str::to_string);
    let mut traces = Vec::new();
    match &state.pool {
        DbPool::Sqlite(pool) => {
            let rows = sqlx::query(LIST_TRACES_SQL_SQLITE)
                .bind(project.clone())
                .bind(project)
                .fetch_all(pool)
                .await
                .map_err(db_error)?;
            for row in &rows {
                traces.push(trace_summary_json!(row));
            }
        }
        DbPool::Postgres(pool) => {
            let rows = sqlx::query(LIST_TRACES_SQL_PG)
                .bind(project)
                .fetch_all(pool)
                .await
                .map_err(db_error)?;
            for row in &rows {
                traces.push(trace_summary_json!(row));
            }
        }
    }
    Ok(Json(traces))
}

async fn list_evals(
    headers: HeaderMap,
    State(state): State<AppState>,
) -> Result<Json<Vec<serde_json::Value>>, StatusCode> {
    let project = authorize(&state, &headers)?.project().map(str::to_string);
    let mut evals = Vec::new();
    match &state.pool {
        DbPool::Sqlite(pool) => {
            let rows = sqlx::query(LIST_EVALS_SQL_SQLITE)
                .bind(project.clone())
                .bind(project)
                .fetch_all(pool)
                .await
                .map_err(db_error)?;
            for row in &rows {
                evals.push(eval_row_json!(row));
            }
        }
        DbPool::Postgres(pool) => {
            let rows = sqlx::query(LIST_EVALS_SQL_PG)
                .bind(project)
                .fetch_all(pool)
                .await
                .map_err(db_error)?;
            for row in &rows {
                evals.push(eval_row_json!(row));
            }
        }
    }
    Ok(Json(evals))
}

async fn get_trace(
    Path(trace_id): Path<String>,
    headers: HeaderMap,
    State(state): State<AppState>,
) -> Result<Json<Vec<serde_json::Value>>, StatusCode> {
    let project = authorize(&state, &headers)?.project().map(str::to_string);
    let mut spans = Vec::new();
    match &state.pool {
        DbPool::Sqlite(pool) => {
            let rows = sqlx::query(GET_TRACE_SQL_SQLITE)
                .bind(trace_id)
                .bind(project.clone())
                .bind(project)
                .fetch_all(pool)
                .await
                .map_err(db_error)?;
            for row in &rows {
                spans.push(span_detail_json!(row));
            }
        }
        DbPool::Postgres(pool) => {
            let rows = sqlx::query(GET_TRACE_SQL_PG)
                .bind(trace_id)
                .bind(project)
                .fetch_all(pool)
                .await
                .map_err(db_error)?;
            for row in &rows {
                spans.push(span_detail_json!(row));
            }
        }
    }
    Ok(Json(spans))
}

use serde::Deserialize;
use trace_weft::hitl::HitlResponse;

async fn get_pending_approvals(
    headers: HeaderMap,
    State(state): State<AppState>,
) -> Result<Json<Vec<String>>, StatusCode> {
    authorize(&state, &headers)?;
    Ok(Json(trace_weft::hitl::get_pending_approvals()))
}

#[derive(Deserialize)]
struct ResolveRequest {
    span_id: String,
    action: String,
    value: Option<serde_json::Value>,
    reason: Option<String>,
}

async fn resolve_approval(
    headers: HeaderMap,
    State(state): State<AppState>,
    Json(req): Json<ResolveRequest>,
) -> Result<StatusCode, StatusCode> {
    authorize(&state, &headers)?;
    let response = if req.action == "approve" {
        HitlResponse::Approved(req.value.unwrap_or(serde_json::json!({})))
    } else {
        HitlResponse::Rejected(req.reason.unwrap_or_else(|| "Rejected by user".to_string()))
    };

    if trace_weft::hitl::resolve_approval(&req.span_id, response).is_ok() {
        Ok(StatusCode::OK)
    } else {
        Err(StatusCode::NOT_FOUND)
    }
}

#[cfg(test)]
mod cors_tests {
    use super::is_allowed_origin;

    #[test]
    fn allows_local_ui_and_tauri_origins() {
        for origin in [
            "http://localhost:5173",
            "http://127.0.0.1:5173",
            "http://localhost:3000",
            "http://localhost",
            "http://127.0.0.1",
            "tauri://localhost",
            "http://tauri.localhost",
        ] {
            assert!(is_allowed_origin(origin), "{origin} should be allowed");
        }
    }

    #[test]
    fn rejects_external_and_lookalike_origins() {
        for origin in [
            "https://evil.example.com",
            "http://localhost.evil.com",
            "http://127.0.0.1.evil.com",
            "https://localhost:5173",
            "http://evil.com",
            "null",
        ] {
            assert!(!is_allowed_origin(origin), "{origin} should be rejected");
        }
    }
}