swink-agent-eval 0.9.0

Evaluation framework for swink-agent: trajectory tracing, golden path verification, and cost governance
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
//! `LangfuseTraceProvider` — pulls a trace (session) from the Langfuse
//! REST API and translates it into a [`RawSession`] (spec 043 T127).
//!
//! Langfuse stores *traces* (sessions) with a tree of *observations* —
//! each observation is typed (`SPAN`, `GENERATION`, `EVENT`) and carries
//! a `metadata` bag keyed by convention. Rather than invent a
//! Langfuse-specific [`crate::trace::mapper::SessionMapper`] variant, we
//! emit each observation as a generic OTel [`SpanData`] so any of the
//! three existing mappers (OpenInference / LangChain / OTel GenAI) can
//! consume the result — consumers decide which vocabulary their
//! instrumentation emits by populating `metadata` accordingly.
//!
//! Transport: `GET {base_url}/api/public/traces/{id}` with HTTP Basic
//! auth (public key : secret key), per the Langfuse public-API spec.
//! A 404 surfaces as [`TraceProviderError::SessionNotFound`]; any
//! observation with `endTime == null` (still in-flight) surfaces as
//! [`TraceProviderError::SessionInProgress`]. Other non-2xx responses
//! map to [`TraceProviderError::BackendFailure`].

#![cfg(feature = "trace-langfuse")]

use std::borrow::Cow;
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use opentelemetry::trace::{
    SpanContext, SpanId, SpanKind, Status, TraceFlags, TraceId, TraceState,
};
use opentelemetry::{InstrumentationScope, KeyValue, Value};
use opentelemetry_sdk::trace::{SpanData, SpanEvents, SpanLinks};
use reqwest::Client;
use serde::Deserialize;

use crate::trace::provider::{RawSession, TraceProvider, TraceProviderError};

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
const LANGFUSE_SESSION_ATTRIBUTE: &str = "session.id";

/// Trace provider backed by the Langfuse public REST API.
///
/// Configure once with the deployment's `base_url` (e.g.
/// `https://cloud.langfuse.com`) plus the `public_key` / `secret_key`
/// pair. `fetch_session` treats `session_id` as a Langfuse *trace id*
/// and issues `GET {base}/api/public/traces/{id}`.
#[derive(Clone, Debug)]
pub struct LangfuseTraceProvider {
    inner: Arc<Inner>,
}

struct Inner {
    http: Client,
    base_url: String,
    public_key: String,
    secret_key: String,
}

impl std::fmt::Debug for Inner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Inner")
            .field("base_url", &self.base_url)
            .field("public_key", &"[REDACTED]")
            .field("secret_key", &"[REDACTED]")
            .finish_non_exhaustive()
    }
}

impl LangfuseTraceProvider {
    /// Build a provider targeting `base_url` authenticated with the
    /// supplied public/secret key pair.
    ///
    /// Trailing slashes on `base_url` are normalized away.
    ///
    /// # Errors
    /// Surfaces any `reqwest::Client::builder()` construction failure
    /// (e.g. TLS backend init) as [`TraceProviderError::BackendFailure`].
    pub fn new(
        base_url: impl Into<String>,
        public_key: impl Into<String>,
        secret_key: impl Into<String>,
    ) -> Result<Self, TraceProviderError> {
        let http = Client::builder()
            .timeout(DEFAULT_TIMEOUT)
            .build()
            .map_err(|err| TraceProviderError::BackendFailure {
                reason: format!("reqwest client build: {err}"),
            })?;
        Ok(Self {
            inner: Arc::new(Inner {
                http,
                base_url: base_url.into().trim_end_matches('/').to_string(),
                public_key: public_key.into(),
                secret_key: secret_key.into(),
            }),
        })
    }

    /// Base URL the provider targets (debug/introspection helper).
    #[must_use]
    pub fn base_url(&self) -> &str {
        &self.inner.base_url
    }
}

// Manual Clone so `Arc::make_mut` would work if we ever add builder
// setters that mutate `Inner`. Keeps symmetry with
// [`crate::trace::otlp::OtlpHttpTraceProvider`].
impl Clone for Inner {
    fn clone(&self) -> Self {
        Self {
            http: self.http.clone(),
            base_url: self.base_url.clone(),
            public_key: self.public_key.clone(),
            secret_key: self.secret_key.clone(),
        }
    }
}

impl TraceProvider for LangfuseTraceProvider {
    fn fetch_session<'a>(
        &'a self,
        session_id: &'a str,
    ) -> crate::trace::provider::TraceProviderFuture<'a> {
        Box::pin(async move {
            let url = format!("{}/api/public/traces/{}", self.inner.base_url, session_id);
            let resp = self
                .inner
                .http
                .get(&url)
                .basic_auth(&self.inner.public_key, Some(&self.inner.secret_key))
                .send()
                .await
                .map_err(|err| TraceProviderError::BackendFailure {
                    reason: format!("langfuse GET {url}: {err}"),
                })?;

            let status = resp.status();
            if status == reqwest::StatusCode::NOT_FOUND {
                return Err(TraceProviderError::SessionNotFound {
                    session_id: session_id.to_string(),
                });
            }
            if !status.is_success() {
                let body = resp.text().await.unwrap_or_default();
                return Err(TraceProviderError::BackendFailure {
                    reason: format!("langfuse http {}: {}", status.as_u16(), truncate(&body)),
                });
            }

            let trace: LangfuseTrace =
                resp.json()
                    .await
                    .map_err(|err| TraceProviderError::BackendFailure {
                        reason: format!("langfuse body parse: {err}"),
                    })?;

            let observations = trace.observations.unwrap_or_default();
            if observations.is_empty() {
                return Err(TraceProviderError::SessionNotFound {
                    session_id: session_id.to_string(),
                });
            }

            let open: usize = observations.iter().filter(|o| o.end_time.is_none()).count();
            if open > 0 {
                return Err(TraceProviderError::SessionInProgress {
                    session_id: session_id.to_string(),
                    open_spans: open,
                });
            }

            let spans = observations
                .into_iter()
                .map(|o| observation_to_span_data(o, session_id, trace.name.as_deref()))
                .collect();

            Ok(RawSession::OtelSpans {
                session_id: session_id.to_string(),
                spans,
            })
        })
    }
}

fn truncate(s: &str) -> String {
    const LIMIT: usize = 512;
    if s.len() <= LIMIT {
        s.to_string()
    } else {
        let mut out = s[..LIMIT].to_string();
        out.push_str("...<truncated>");
        out
    }
}

// ─── Langfuse wire types ────────────────────────────────────────────────────

#[derive(Debug, Deserialize)]
struct LangfuseTrace {
    #[serde(default)]
    id: Option<String>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    observations: Option<Vec<LangfuseObservation>>,
}

impl LangfuseTrace {
    #[allow(dead_code)]
    fn id(&self) -> &str {
        self.id.as_deref().unwrap_or_default()
    }
}

#[derive(Debug, Deserialize)]
struct LangfuseObservation {
    #[serde(default)]
    id: Option<String>,
    #[serde(default, rename = "parentObservationId")]
    parent_observation_id: Option<String>,
    #[serde(default)]
    name: Option<String>,
    /// `SPAN` | `GENERATION` | `EVENT`. We only use this hint to choose
    /// `SpanKind` — unrecognized types map to `Internal`.
    #[serde(default, rename = "type")]
    obs_type: Option<String>,
    #[serde(default, rename = "startTime")]
    start_time: Option<String>,
    #[serde(default, rename = "endTime")]
    end_time: Option<String>,
    /// Free-form user-supplied metadata. Each key is promoted to an OTel
    /// attribute on the emitted span so downstream mappers can consume it.
    #[serde(default)]
    metadata: Option<serde_json::Value>,
    /// Langfuse-native fields a mapper may consult directly; surfaced as
    /// OTel attrs under a `langfuse.*` prefix so mappers that don't care
    /// can ignore them.
    #[serde(default)]
    model: Option<String>,
    #[serde(default, rename = "promptTokens")]
    prompt_tokens: Option<u64>,
    #[serde(default, rename = "completionTokens")]
    completion_tokens: Option<u64>,
    #[serde(default, rename = "totalTokens")]
    total_tokens: Option<u64>,
    #[serde(default)]
    output: Option<serde_json::Value>,
}

fn observation_to_span_data(
    obs: LangfuseObservation,
    session_id: &str,
    trace_name: Option<&str>,
) -> SpanData {
    let name = obs
        .name
        .or_else(|| trace_name.map(str::to_string))
        .unwrap_or_else(|| "langfuse.observation".to_string());

    let start = obs
        .start_time
        .as_deref()
        .and_then(parse_rfc3339)
        .unwrap_or_else(SystemTime::now);
    let end = obs
        .end_time
        .as_deref()
        .and_then(parse_rfc3339)
        .unwrap_or(start + Duration::from_millis(1));

    let span_id = obs.id.as_deref().map_or(SpanId::INVALID, hash_to_span_id);
    let parent_span_id = obs
        .parent_observation_id
        .as_deref()
        .map_or(SpanId::INVALID, hash_to_span_id);
    let trace_id = hash_to_trace_id(session_id);

    let span_context = SpanContext::new(
        trace_id,
        span_id,
        TraceFlags::default(),
        false,
        TraceState::default(),
    );

    let span_kind = match obs.obs_type.as_deref() {
        Some("GENERATION") => SpanKind::Client,
        _ => SpanKind::Internal,
    };

    let mut attributes: Vec<KeyValue> = Vec::new();
    attributes.push(KeyValue::new(
        LANGFUSE_SESSION_ATTRIBUTE,
        session_id.to_string(),
    ));
    if let Some(model) = obs.model {
        attributes.push(KeyValue::new("langfuse.model", model));
    }
    if let Some(t) = obs.prompt_tokens {
        attributes.push(KeyValue::new(
            "langfuse.usage.prompt_tokens",
            i64::try_from(t).unwrap_or(i64::MAX),
        ));
    }
    if let Some(t) = obs.completion_tokens {
        attributes.push(KeyValue::new(
            "langfuse.usage.completion_tokens",
            i64::try_from(t).unwrap_or(i64::MAX),
        ));
    }
    if let Some(t) = obs.total_tokens {
        attributes.push(KeyValue::new(
            "langfuse.usage.total_tokens",
            i64::try_from(t).unwrap_or(i64::MAX),
        ));
    }
    if let Some(out) = &obs.output
        && let Some(text) = output_text(out)
    {
        attributes.push(KeyValue::new("langfuse.output.text", text));
    }
    if let Some(meta) = obs.metadata
        && let Some(map) = meta.as_object()
    {
        for (k, v) in map {
            if let Some(value) = json_to_otel_value(v) {
                attributes.push(KeyValue::new(k.clone(), value));
            }
        }
    }

    SpanData {
        span_context,
        parent_span_id,
        parent_span_is_remote: false,
        span_kind,
        name: Cow::Owned(name),
        start_time: start,
        end_time: end,
        attributes,
        dropped_attributes_count: 0,
        events: SpanEvents::default(),
        links: SpanLinks::default(),
        status: Status::Unset,
        instrumentation_scope: InstrumentationScope::builder("langfuse").build(),
    }
}

/// Minimal RFC 3339 parser that covers the shapes Langfuse emits —
/// `YYYY-MM-DDTHH:MM:SS[.fraction][Z | +HH:MM | -HH:MM]`. Returns `None`
/// on anything we can't confidently parse; callers fall back to "now".
///
/// Hand-rolled to avoid pulling `chrono` into the eval default build
/// (SC-009). We only need seconds-precision round-trips for tests.
fn parse_rfc3339(s: &str) -> Option<SystemTime> {
    // Split into date portion, time portion, and timezone suffix.
    let (date, rest) = s.split_once('T')?;
    let mut date_parts = date.splitn(3, '-');
    let year: i64 = date_parts.next()?.parse().ok()?;
    let month: u32 = date_parts.next()?.parse().ok()?;
    let day: u32 = date_parts.next()?.parse().ok()?;

    let (time_part, tz_part) = split_tz(rest)?;

    // Strip fractional seconds if present.
    let (hms, frac_nanos) = match time_part.split_once('.') {
        Some((hms, frac)) => (hms, parse_fraction_to_nanos(frac)),
        None => (time_part, 0),
    };
    let mut hms_parts = hms.splitn(3, ':');
    let hour: u32 = hms_parts.next()?.parse().ok()?;
    let minute: u32 = hms_parts.next()?.parse().ok()?;
    let second: u32 = hms_parts.next()?.parse().ok()?;

    let tz_offset_seconds = parse_tz_offset(tz_part)?;

    // Compute days since 1970-01-01 for (year, month, day) using Howard
    // Hinnant's `days_from_civil` — exact and branch-cheap.
    let days = days_from_civil(year, month, day);
    let secs_in_day =
        i64::from(hour) * 3600 + i64::from(minute) * 60 + i64::from(second) - tz_offset_seconds;
    let total_seconds = days * 86_400 + secs_in_day;
    if total_seconds < 0 {
        return None;
    }
    #[allow(clippy::cast_sign_loss)]
    let unix_seconds = total_seconds as u64;
    Some(UNIX_EPOCH + Duration::new(unix_seconds, frac_nanos))
}

fn split_tz(rest: &str) -> Option<(&str, &str)> {
    if let Some(idx) = rest.find(['Z', 'z']) {
        return Some((&rest[..idx], "Z"));
    }
    // Find a `+` or `-` that starts a timezone offset (must be followed
    // by digits-and-colon; not part of a negative fractional second).
    let bytes = rest.as_bytes();
    for (i, &b) in bytes.iter().enumerate().rev() {
        if (b == b'+' || b == b'-') && i > 0 {
            return Some((&rest[..i], &rest[i..]));
        }
    }
    None
}

fn parse_fraction_to_nanos(frac: &str) -> u32 {
    // Accept at most 9 digits; shorter fractions are padded, longer are truncated.
    let digits: String = frac.chars().take_while(char::is_ascii_digit).collect();
    if digits.is_empty() {
        return 0;
    }
    let mut padded = digits;
    while padded.len() < 9 {
        padded.push('0');
    }
    padded[..9].parse::<u32>().unwrap_or(0)
}

fn parse_tz_offset(tz: &str) -> Option<i64> {
    if tz == "Z" || tz == "z" {
        return Some(0);
    }
    let sign = match tz.as_bytes().first()? {
        b'+' => 1,
        b'-' => -1,
        _ => return None,
    };
    let body = &tz[1..];
    let (h, m) = body.split_once(':').unwrap_or((body, "0"));
    let h: i64 = h.parse().ok()?;
    let m: i64 = m.parse().ok()?;
    Some(sign * (h * 3600 + m * 60))
}

/// Howard Hinnant's `days_from_civil`: returns days since 1970-01-01
/// for any proleptic Gregorian date. Exact for the full `i64` range;
/// no dependency on `chrono`.
#[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
fn days_from_civil(y: i64, m: u32, d: u32) -> i64 {
    let m_i = i64::from(m);
    let d_i = i64::from(d);
    let y = if m_i <= 2 { y - 1 } else { y };
    let era = y.div_euclid(400);
    let yoe = y.rem_euclid(400);
    let doy = (153 * (if m_i > 2 { m_i - 3 } else { m_i + 9 }) + 2) / 5 + d_i - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    era * 146_097 + doe - 719_468
}

fn output_text(v: &serde_json::Value) -> Option<String> {
    match v {
        serde_json::Value::String(s) => Some(s.clone()),
        serde_json::Value::Object(_) | serde_json::Value::Array(_) => Some(v.to_string()),
        _ => None,
    }
}

fn json_to_otel_value(v: &serde_json::Value) -> Option<Value> {
    Some(match v {
        serde_json::Value::String(s) => Value::String(s.clone().into()),
        serde_json::Value::Bool(b) => Value::Bool(*b),
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                Value::I64(i)
            } else if let Some(u) = n.as_u64() {
                Value::I64(i64::try_from(u).unwrap_or(i64::MAX))
            } else if let Some(f) = n.as_f64() {
                Value::F64(f)
            } else {
                return None;
            }
        }
        serde_json::Value::Null => return None,
        other => Value::String(other.to_string().into()),
    })
}

// Langfuse ids are opaque strings (commonly UUIDs); fold them into an
// OTel `SpanId` / `TraceId` deterministically so round-trips are stable.
// We use SHA-256 and truncate — not cryptographic here, just used to
// give the OTel types non-zero bytes that preserve distinctness across
// different langfuse ids.
fn hash_to_span_id(s: &str) -> SpanId {
    use sha2::{Digest, Sha256};
    let digest = Sha256::digest(s.as_bytes());
    let mut bytes = [0u8; 8];
    bytes.copy_from_slice(&digest[..8]);
    SpanId::from_bytes(bytes)
}

fn hash_to_trace_id(s: &str) -> TraceId {
    use sha2::{Digest, Sha256};
    let digest = Sha256::digest(s.as_bytes());
    let mut bytes = [0u8; 16];
    bytes.copy_from_slice(&digest[..16]);
    TraceId::from_bytes(bytes)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn hash_to_span_id_is_deterministic() {
        let a = hash_to_span_id("obs-1");
        let b = hash_to_span_id("obs-1");
        assert_eq!(a, b);
        let c = hash_to_span_id("obs-2");
        assert_ne!(a, c);
    }

    #[test]
    fn langfuse_provider_debug_redacts_auth_keys() {
        let provider =
            LangfuseTraceProvider::new("https://langfuse.example", "pk-secret", "sk-secret")
                .expect("provider builds");

        let debug = format!("{provider:?}");

        assert!(
            !debug.contains("pk-secret"),
            "Debug leaks Langfuse public key"
        );
        assert!(
            !debug.contains("sk-secret"),
            "Debug leaks Langfuse secret key"
        );
        assert!(debug.contains("[REDACTED]"));
        assert!(debug.contains("https://langfuse.example"));
    }

    #[test]
    fn parse_rfc3339_roundtrips() {
        let t = parse_rfc3339("2026-04-23T10:00:00Z").expect("rfc3339");
        let later = parse_rfc3339("2026-04-23T10:00:01Z").expect("rfc3339");
        assert!(later > t);
    }

    #[test]
    fn json_to_otel_value_maps_primitives() {
        assert!(matches!(
            json_to_otel_value(&serde_json::json!("hi")),
            Some(Value::String(_))
        ));
        assert!(matches!(
            json_to_otel_value(&serde_json::json!(true)),
            Some(Value::Bool(true))
        ));
        assert!(matches!(
            json_to_otel_value(&serde_json::json!(42)),
            Some(Value::I64(42))
        ));
        assert!(matches!(
            json_to_otel_value(&serde_json::json!(2.5_f64)),
            Some(Value::F64(_))
        ));
        assert!(json_to_otel_value(&serde_json::Value::Null).is_none());
    }
}