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
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
//! `SessionMapper` trait and built-in implementations.
//!
//! Session mappers translate a backend-neutral [`RawSession`] (produced by a
//! [`TraceProvider`](crate::trace::provider::TraceProvider)) into an internal
//! [`Invocation`]. Each mapper encodes one semantic convention:
//!
//! * [`OpenInferenceSessionMapper`] — OpenInference / Arize attribute vocabulary.
//! * [`LangChainSessionMapper`] — LangChain-OTel attribute vocabulary.
//! * [`OtelGenAiSessionMapper`] — OpenTelemetry GenAI semantic conventions,
//!   with per-version attribute tables (v1.27, v1.30, experimental) per
//!   research R-005 and FR-032.
//!
//! Missing-but-required attributes surface as
//! [`MappingError::MissingAttribute`] — mappers never panic on bad input
//! (spec 043 edge case under FR-031/FR-032).

use std::collections::HashMap;
use std::time::Duration;

use opentelemetry::Value;
use opentelemetry_sdk::trace::SpanData;
use swink_agent::{AssistantMessage, ContentBlock, Cost, ModelSpec, StopReason, Usage};
use thiserror::Error;

use crate::trace::provider::RawSession;
use crate::types::{Invocation, RecordedToolCall, TurnRecord};

// ─── Error model ────────────────────────────────────────────────────────────

/// Errors a [`SessionMapper`] can surface. No mapper variant panics on
/// missing data (spec 043 FR-031 edge case).
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum MappingError {
    /// A required attribute was absent on every span in the session.
    #[error("required attribute `{name}` not found on any span in session")]
    MissingAttribute {
        /// Fully-qualified attribute key per the backend's convention.
        name: String,
    },

    /// The raw session shape is not one this mapper knows how to handle.
    #[error("raw session shape is unsupported by this mapper: {reason}")]
    UnsupportedShape {
        /// Human-readable diagnostic.
        reason: String,
    },

    /// An attribute value was present but could not be parsed into the
    /// expected Rust type (e.g. a token count that wasn't an integer).
    #[error("attribute `{name}` has unexpected value: {reason}")]
    InvalidAttribute {
        /// Fully-qualified attribute key.
        name: String,
        /// Why the value failed to parse.
        reason: String,
    },
}

// ─── Trait ──────────────────────────────────────────────────────────────────

/// Translate an external-convention [`RawSession`] into an [`Invocation`].
///
/// Mappers SHOULD be stateless and cheap to clone; the runner instantiates
/// them once per backend configuration and shares them across cases.
pub trait SessionMapper: Send + Sync {
    /// Produce an `Invocation` from the raw payload. MUST return
    /// [`MappingError::MissingAttribute`] rather than panic when required
    /// attributes are absent.
    fn map(&self, raw: &RawSession) -> Result<Invocation, MappingError>;
}

// ─── Shared helpers ─────────────────────────────────────────────────────────

// `RawSession` is `#[non_exhaustive]`; keeping a `Result` return type lets
// future backend-specific variants surface `UnsupportedShape` without
// reshaping every caller.
#[allow(clippy::unnecessary_wraps)]
fn spans_of(raw: &RawSession) -> Result<&[SpanData], MappingError> {
    match raw {
        RawSession::OtelSpans { spans, .. } => Ok(spans),
    }
}

/// Find the first span carrying attribute `key` and return the attribute
/// value as a borrowed string slice.
fn first_string_attr(spans: &[SpanData], key: &str) -> Option<String> {
    for span in spans {
        for kv in &span.attributes {
            if kv.key.as_str() == key {
                return Some(kv.value.as_str().into_owned());
            }
        }
    }
    None
}

/// Sum a numeric attribute across every span that carries it.
fn sum_u64_attr(spans: &[SpanData], key: &str) -> Result<u64, MappingError> {
    let mut sum: u64 = 0;
    for span in spans {
        for kv in &span.attributes {
            if kv.key.as_str() == key {
                let parsed =
                    match &kv.value {
                        Value::I64(v) => {
                            u64::try_from(*v).map_err(|_| MappingError::InvalidAttribute {
                                name: key.to_string(),
                                reason: format!("negative token count: {v}"),
                            })?
                        }
                        Value::String(s) => s.as_str().parse::<u64>().map_err(|e| {
                            MappingError::InvalidAttribute {
                                name: key.to_string(),
                                reason: format!("not u64: {e}"),
                            }
                        })?,
                        other => {
                            return Err(MappingError::InvalidAttribute {
                                name: key.to_string(),
                                reason: format!("expected integer, got {other:?}"),
                            });
                        }
                    };
                sum = sum.saturating_add(parsed);
            }
        }
    }
    Ok(sum)
}

fn total_duration(spans: &[SpanData]) -> Duration {
    spans
        .iter()
        .filter_map(|s| s.end_time.duration_since(s.start_time).ok())
        .sum()
}

fn current_ts() -> u64 {
    use std::time::{SystemTime, UNIX_EPOCH};
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| u64::try_from(d.as_millis()).unwrap_or(u64::MAX))
}

/// Build a minimal `AssistantMessage` from a provider/model pair and optional
/// final response text. Used by every mapper to produce a single terminal
/// turn that carries the model-identifying attributes.
fn assistant_message(
    provider: &str,
    model: &str,
    response_text: Option<String>,
    usage: Usage,
) -> AssistantMessage {
    let mut content: Vec<ContentBlock> = Vec::new();
    if let Some(text) = response_text
        && !text.is_empty()
    {
        content.push(ContentBlock::Text { text });
    }
    AssistantMessage {
        content,
        provider: provider.to_string(),
        model_id: model.to_string(),
        usage,
        cost: Cost::default(),
        stop_reason: StopReason::Stop,
        error_message: None,
        error_kind: None,
        timestamp: current_ts(),
        cache_hint: None,
    }
}

/// Extract `RecordedToolCall`s from tool-kind spans using the conventional
/// `<prefix>.name` / `<prefix>.arguments` attribute pair (e.g.
/// `tool.name` / `tool.arguments` under GenAI, or `llm.tool_call.*` under
/// OpenInference). The caller supplies the `prefix` and a span-name hint.
fn extract_tool_calls(
    spans: &[SpanData],
    span_name_hint: &str,
    name_attr: &str,
    args_attr: &str,
    id_attr: Option<&str>,
) -> Vec<RecordedToolCall> {
    let mut calls = Vec::new();
    for span in spans {
        if !span.name.contains(span_name_hint) {
            continue;
        }
        let name = span
            .attributes
            .iter()
            .find(|kv| kv.key.as_str() == name_attr)
            .map(|kv| kv.value.as_str().into_owned());
        let Some(name) = name else { continue };

        let arguments = span
            .attributes
            .iter()
            .find(|kv| kv.key.as_str() == args_attr)
            .and_then(|kv| {
                let raw = kv.value.as_str();
                serde_json::from_str::<serde_json::Value>(&raw).ok()
            })
            .unwrap_or(serde_json::Value::Null);

        let id = id_attr
            .and_then(|k| {
                span.attributes
                    .iter()
                    .find(|kv| kv.key.as_str() == k)
                    .map(|kv| kv.value.as_str().into_owned())
            })
            .unwrap_or_else(|| span.span_context.span_id().to_string());

        calls.push(RecordedToolCall {
            id,
            name,
            arguments,
        });
    }
    calls
}

/// Shared backbone: build an `Invocation` from the provided provider/model,
/// usage, response, and tool calls. All three built-in mappers converge on
/// this so they return consistent shapes.
fn build_invocation(
    spans: &[SpanData],
    provider: String,
    model: String,
    usage: Usage,
    response_text: Option<String>,
    tool_calls: Vec<RecordedToolCall>,
) -> Invocation {
    let total_duration = total_duration(spans);
    let assistant = assistant_message(&provider, &model, response_text.clone(), usage.clone());
    let turn = TurnRecord {
        turn_index: 0,
        assistant_message: assistant.clone(),
        tool_calls,
        tool_results: Vec::new(),
        duration: total_duration,
    };
    Invocation {
        turns: vec![turn],
        total_usage: usage,
        total_cost: Cost::default(),
        total_duration,
        final_response: response_text,
        stop_reason: assistant.stop_reason,
        model: ModelSpec::new(provider, model),
    }
}

// ─── OpenInferenceSessionMapper (T123) ──────────────────────────────────────

/// Mapper for the OpenInference / Arize attribute vocabulary.
///
/// Consumed attribute keys (subset we require):
/// * `llm.provider`, `llm.model_name` — required; absence yields
///   [`MappingError::MissingAttribute`].
/// * `llm.token_count.prompt`, `llm.token_count.completion` — optional usage.
/// * `output.value` — optional final response text.
/// * Tool spans: `tool.name`, `tool.parameters`, `tool.call_id`.
#[derive(Debug, Clone, Copy, Default)]
pub struct OpenInferenceSessionMapper;

impl OpenInferenceSessionMapper {
    pub const PROVIDER_KEY: &'static str = "llm.provider";
    pub const MODEL_KEY: &'static str = "llm.model_name";
    pub const INPUT_TOKENS_KEY: &'static str = "llm.token_count.prompt";
    pub const OUTPUT_TOKENS_KEY: &'static str = "llm.token_count.completion";
    pub const RESPONSE_KEY: &'static str = "output.value";
    pub const TOOL_NAME_KEY: &'static str = "tool.name";
    pub const TOOL_PARAMS_KEY: &'static str = "tool.parameters";
    pub const TOOL_ID_KEY: &'static str = "tool.call_id";
}

impl SessionMapper for OpenInferenceSessionMapper {
    fn map(&self, raw: &RawSession) -> Result<Invocation, MappingError> {
        let spans = spans_of(raw)?;

        let provider = first_string_attr(spans, Self::PROVIDER_KEY).ok_or_else(|| {
            MappingError::MissingAttribute {
                name: Self::PROVIDER_KEY.to_string(),
            }
        })?;
        let model = first_string_attr(spans, Self::MODEL_KEY).ok_or_else(|| {
            MappingError::MissingAttribute {
                name: Self::MODEL_KEY.to_string(),
            }
        })?;

        let input = sum_u64_attr(spans, Self::INPUT_TOKENS_KEY)?;
        let output = sum_u64_attr(spans, Self::OUTPUT_TOKENS_KEY)?;
        let usage = Usage {
            input,
            output,
            total: input.saturating_add(output),
            ..Usage::default()
        };
        let response = first_string_attr(spans, Self::RESPONSE_KEY);

        let tool_calls = extract_tool_calls(
            spans,
            "tool",
            Self::TOOL_NAME_KEY,
            Self::TOOL_PARAMS_KEY,
            Some(Self::TOOL_ID_KEY),
        );

        Ok(build_invocation(
            spans, provider, model, usage, response, tool_calls,
        ))
    }
}

// ─── LangChainSessionMapper (T124) ──────────────────────────────────────────

/// Mapper for LangChain-OTel traces.
///
/// Consumed attribute keys:
/// * `langchain.llm.provider`, `langchain.llm.model` — required.
/// * `langchain.llm.usage.prompt_tokens`, `langchain.llm.usage.completion_tokens`
///   — optional usage.
/// * `langchain.llm.output_text` — optional final response.
/// * Tool spans: `langchain.tool.name`, `langchain.tool.input`.
#[derive(Debug, Clone, Copy, Default)]
pub struct LangChainSessionMapper;

impl LangChainSessionMapper {
    pub const PROVIDER_KEY: &'static str = "langchain.llm.provider";
    pub const MODEL_KEY: &'static str = "langchain.llm.model";
    pub const INPUT_TOKENS_KEY: &'static str = "langchain.llm.usage.prompt_tokens";
    pub const OUTPUT_TOKENS_KEY: &'static str = "langchain.llm.usage.completion_tokens";
    pub const RESPONSE_KEY: &'static str = "langchain.llm.output_text";
    pub const TOOL_NAME_KEY: &'static str = "langchain.tool.name";
    pub const TOOL_INPUT_KEY: &'static str = "langchain.tool.input";
    pub const TOOL_ID_KEY: &'static str = "langchain.tool.run_id";
}

impl SessionMapper for LangChainSessionMapper {
    fn map(&self, raw: &RawSession) -> Result<Invocation, MappingError> {
        let spans = spans_of(raw)?;

        let provider = first_string_attr(spans, Self::PROVIDER_KEY).ok_or_else(|| {
            MappingError::MissingAttribute {
                name: Self::PROVIDER_KEY.to_string(),
            }
        })?;
        let model = first_string_attr(spans, Self::MODEL_KEY).ok_or_else(|| {
            MappingError::MissingAttribute {
                name: Self::MODEL_KEY.to_string(),
            }
        })?;

        let input = sum_u64_attr(spans, Self::INPUT_TOKENS_KEY)?;
        let output = sum_u64_attr(spans, Self::OUTPUT_TOKENS_KEY)?;
        let usage = Usage {
            input,
            output,
            total: input.saturating_add(output),
            ..Usage::default()
        };
        let response = first_string_attr(spans, Self::RESPONSE_KEY);

        let tool_calls = extract_tool_calls(
            spans,
            "tool",
            Self::TOOL_NAME_KEY,
            Self::TOOL_INPUT_KEY,
            Some(Self::TOOL_ID_KEY),
        );

        Ok(build_invocation(
            spans, provider, model, usage, response, tool_calls,
        ))
    }
}

// ─── OtelGenAiSessionMapper + version enum (T125) ───────────────────────────

/// OpenTelemetry GenAI semantic-convention versions supported by
/// [`OtelGenAiSessionMapper`] (research R-005, FR-032).
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GenAIConventionVersion {
    /// `gen_ai.*` vocabulary as of OTel semantic conventions v1.27.
    V1_27,
    /// v1.30 — introduces `gen_ai.operation.name` and renames some response
    /// attributes.
    V1_30,
    /// Forward-compatible bucket: the mapper consumes the v1.30 attribute
    /// set and tolerates unknown extras.
    Experimental,
}

/// Attribute-key lookup table for a single GenAI convention version.
///
/// Exposed (rather than hidden inside the mapper) so out-of-tree callers can
/// inspect exactly which keys are consumed per version.
#[derive(Debug, Clone)]
pub struct GenAIAttributeTable {
    pub system: &'static str,
    pub request_model: &'static str,
    pub response_model: &'static str,
    pub input_tokens: &'static str,
    pub output_tokens: &'static str,
    pub finish_reasons: &'static str,
    pub response_text: &'static str,
    pub tool_name: &'static str,
    pub tool_arguments: &'static str,
    pub tool_id: &'static str,
}

impl GenAIAttributeTable {
    /// Per-version attribute table (FR-032).
    #[must_use]
    pub const fn for_version(version: GenAIConventionVersion) -> Self {
        match version {
            GenAIConventionVersion::V1_27 => Self {
                system: "gen_ai.system",
                request_model: "gen_ai.request.model",
                response_model: "gen_ai.response.model",
                input_tokens: "gen_ai.usage.input_tokens",
                output_tokens: "gen_ai.usage.output_tokens",
                finish_reasons: "gen_ai.response.finish_reasons",
                response_text: "gen_ai.completion.content",
                tool_name: "gen_ai.tool.name",
                tool_arguments: "gen_ai.tool.call.arguments",
                tool_id: "gen_ai.tool.call.id",
            },
            GenAIConventionVersion::V1_30 | GenAIConventionVersion::Experimental => Self {
                system: "gen_ai.system",
                request_model: "gen_ai.request.model",
                response_model: "gen_ai.response.model",
                input_tokens: "gen_ai.usage.input_tokens",
                output_tokens: "gen_ai.usage.output_tokens",
                finish_reasons: "gen_ai.response.finish_reasons",
                response_text: "gen_ai.output.messages",
                tool_name: "gen_ai.tool.name",
                tool_arguments: "gen_ai.tool.arguments",
                tool_id: "gen_ai.tool.call.id",
            },
        }
    }
}

/// Mapper for OTel GenAI semantic conventions.
///
/// Construction pins a [`GenAIConventionVersion`]; swapping versions requires
/// no code change on the caller side — just re-construct with a different
/// enum variant (FR-032 independent-test criterion).
#[derive(Debug, Clone)]
pub struct OtelGenAiSessionMapper {
    /// Convention version this mapper targets.
    pub version: GenAIConventionVersion,
    table: GenAIAttributeTable,
}

impl OtelGenAiSessionMapper {
    /// Build a mapper pinned to `version`.
    #[must_use]
    pub fn new(version: GenAIConventionVersion) -> Self {
        Self {
            version,
            table: GenAIAttributeTable::for_version(version),
        }
    }

    /// Attribute table this mapper uses.
    #[must_use]
    pub fn table(&self) -> &GenAIAttributeTable {
        &self.table
    }
}

impl Default for OtelGenAiSessionMapper {
    fn default() -> Self {
        Self::new(GenAIConventionVersion::V1_30)
    }
}

impl SessionMapper for OtelGenAiSessionMapper {
    fn map(&self, raw: &RawSession) -> Result<Invocation, MappingError> {
        let spans = spans_of(raw)?;
        let tbl = &self.table;

        let provider =
            first_string_attr(spans, tbl.system).ok_or_else(|| MappingError::MissingAttribute {
                name: tbl.system.to_string(),
            })?;
        let model = first_string_attr(spans, tbl.response_model)
            .or_else(|| first_string_attr(spans, tbl.request_model))
            .ok_or_else(|| MappingError::MissingAttribute {
                name: tbl.request_model.to_string(),
            })?;

        let input = sum_u64_attr(spans, tbl.input_tokens)?;
        let output = sum_u64_attr(spans, tbl.output_tokens)?;
        let usage = Usage {
            input,
            output,
            total: input.saturating_add(output),
            ..Usage::default()
        };
        let response = first_string_attr(spans, tbl.response_text);

        let tool_calls = extract_tool_calls(
            spans,
            "tool",
            tbl.tool_name,
            tbl.tool_arguments,
            Some(tbl.tool_id),
        );

        // Unknown attributes are tolerated for Experimental. We surface them
        // as a debug log but never error out.
        if matches!(self.version, GenAIConventionVersion::Experimental) {
            let known: [&str; 10] = [
                tbl.system,
                tbl.request_model,
                tbl.response_model,
                tbl.input_tokens,
                tbl.output_tokens,
                tbl.finish_reasons,
                tbl.response_text,
                tbl.tool_name,
                tbl.tool_arguments,
                tbl.tool_id,
            ];
            let mut seen_unknown: HashMap<String, usize> = HashMap::new();
            for span in spans {
                for kv in &span.attributes {
                    let k = kv.key.as_str();
                    if k.starts_with("gen_ai.") && !known.contains(&k) {
                        *seen_unknown.entry(k.to_string()).or_default() += 1;
                    }
                }
            }
            if !seen_unknown.is_empty() {
                tracing::debug!(
                    target: "swink_agent_eval::trace::mapper",
                    unknown = ?seen_unknown,
                    "OtelGenAiSessionMapper (Experimental) tolerating unknown gen_ai.* attributes"
                );
            }
        }

        Ok(build_invocation(
            spans, provider, model, usage, response, tool_calls,
        ))
    }
}

// ─── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use opentelemetry::trace::{
        SpanContext, SpanId, SpanKind, Status, TraceFlags, TraceId, TraceState,
    };
    use opentelemetry::{InstrumentationScope, KeyValue};
    use opentelemetry_sdk::trace::{SpanEvents, SpanLinks};
    use std::borrow::Cow;
    use std::time::{Duration, SystemTime};

    fn make_span(name: &str, attrs: Vec<KeyValue>) -> SpanData {
        let start = SystemTime::now();
        SpanData {
            span_context: SpanContext::new(
                TraceId::from(7_u128),
                SpanId::from(7_u64),
                TraceFlags::default(),
                false,
                TraceState::default(),
            ),
            parent_span_id: SpanId::INVALID,
            parent_span_is_remote: false,
            span_kind: SpanKind::Internal,
            name: Cow::Owned(name.to_string()),
            start_time: start,
            end_time: start + Duration::from_millis(5),
            attributes: attrs,
            dropped_attributes_count: 0,
            events: SpanEvents::default(),
            links: SpanLinks::default(),
            status: Status::Unset,
            instrumentation_scope: InstrumentationScope::builder("test").build(),
        }
    }

    fn session(spans: Vec<SpanData>) -> RawSession {
        RawSession::OtelSpans {
            session_id: "s".into(),
            spans,
        }
    }

    #[test]
    fn openinference_missing_provider_returns_missing_attribute() {
        let raw = session(vec![make_span(
            "llm",
            vec![KeyValue::new("llm.model_name", "m")],
        )]);
        let err = OpenInferenceSessionMapper
            .map(&raw)
            .expect_err("provider absent");
        match err {
            MappingError::MissingAttribute { name } => {
                assert_eq!(name, OpenInferenceSessionMapper::PROVIDER_KEY);
            }
            other => panic!("expected MissingAttribute, got {other:?}"),
        }
    }

    #[test]
    fn openinference_builds_invocation_with_usage_and_tool_calls() {
        let llm = make_span(
            "llm",
            vec![
                KeyValue::new("llm.provider", "anthropic"),
                KeyValue::new("llm.model_name", "claude-3"),
                KeyValue::new("llm.token_count.prompt", 10_i64),
                KeyValue::new("llm.token_count.completion", 20_i64),
                KeyValue::new("output.value", "hello"),
            ],
        );
        let tool = make_span(
            "tool.exec",
            vec![
                KeyValue::new("tool.name", "read_file"),
                KeyValue::new("tool.parameters", r#"{"path":"/etc"}"#),
                KeyValue::new("tool.call_id", "call_42"),
            ],
        );
        let inv = OpenInferenceSessionMapper
            .map(&session(vec![llm, tool]))
            .unwrap();
        assert_eq!(inv.model.provider, "anthropic");
        assert_eq!(inv.model.model_id, "claude-3");
        assert_eq!(inv.total_usage.input, 10);
        assert_eq!(inv.total_usage.output, 20);
        assert_eq!(inv.total_usage.total, 30);
        assert_eq!(inv.final_response.as_deref(), Some("hello"));
        assert_eq!(inv.turns.len(), 1);
        assert_eq!(inv.turns[0].tool_calls.len(), 1);
        assert_eq!(inv.turns[0].tool_calls[0].name, "read_file");
        assert_eq!(inv.turns[0].tool_calls[0].id, "call_42");
    }

    #[test]
    fn langchain_missing_model_returns_missing_attribute() {
        let raw = session(vec![make_span(
            "chain",
            vec![KeyValue::new("langchain.llm.provider", "openai")],
        )]);
        let err = LangChainSessionMapper.map(&raw).expect_err("model absent");
        match err {
            MappingError::MissingAttribute { name } => {
                assert_eq!(name, LangChainSessionMapper::MODEL_KEY);
            }
            other => panic!("expected MissingAttribute, got {other:?}"),
        }
    }

    #[test]
    fn langchain_round_trips_provider_and_tokens() {
        let raw = session(vec![make_span(
            "chain",
            vec![
                KeyValue::new("langchain.llm.provider", "openai"),
                KeyValue::new("langchain.llm.model", "gpt-4"),
                KeyValue::new("langchain.llm.usage.prompt_tokens", 3_i64),
                KeyValue::new("langchain.llm.usage.completion_tokens", 4_i64),
            ],
        )]);
        let inv = LangChainSessionMapper.map(&raw).unwrap();
        assert_eq!(inv.model.provider, "openai");
        assert_eq!(inv.model.model_id, "gpt-4");
        assert_eq!(inv.total_usage.total, 7);
    }

    #[test]
    fn genai_v1_27_and_v1_30_have_distinct_response_keys() {
        let t27 = GenAIAttributeTable::for_version(GenAIConventionVersion::V1_27);
        let t30 = GenAIAttributeTable::for_version(GenAIConventionVersion::V1_30);
        assert_ne!(t27.response_text, t30.response_text);
        assert_eq!(t27.system, t30.system); // `gen_ai.system` stable across versions.
    }

    #[test]
    fn genai_missing_system_returns_missing_attribute() {
        let raw = session(vec![make_span(
            "llm.call",
            vec![KeyValue::new("gen_ai.request.model", "m")],
        )]);
        let err = OtelGenAiSessionMapper::new(GenAIConventionVersion::V1_30)
            .map(&raw)
            .expect_err("system absent");
        assert!(matches!(err, MappingError::MissingAttribute { name } if name == "gen_ai.system"));
    }

    #[test]
    fn genai_v1_30_maps_usage_and_tool_call() {
        let llm = make_span(
            "llm.call",
            vec![
                KeyValue::new("gen_ai.system", "anthropic"),
                KeyValue::new("gen_ai.request.model", "claude-3"),
                KeyValue::new("gen_ai.usage.input_tokens", 5_i64),
                KeyValue::new("gen_ai.usage.output_tokens", 6_i64),
            ],
        );
        let tool = make_span(
            "tool.call",
            vec![
                KeyValue::new("gen_ai.tool.name", "search"),
                KeyValue::new("gen_ai.tool.arguments", r#"{"q":"rust"}"#),
                KeyValue::new("gen_ai.tool.call.id", "tc_1"),
            ],
        );
        let inv = OtelGenAiSessionMapper::new(GenAIConventionVersion::V1_30)
            .map(&session(vec![llm, tool]))
            .unwrap();
        assert_eq!(inv.total_usage.input, 5);
        assert_eq!(inv.total_usage.output, 6);
        assert_eq!(inv.turns[0].tool_calls[0].name, "search");
    }

    #[test]
    fn genai_experimental_tolerates_unknown_attributes() {
        let llm = make_span(
            "llm.call",
            vec![
                KeyValue::new("gen_ai.system", "openai"),
                KeyValue::new("gen_ai.request.model", "gpt-5"),
                KeyValue::new("gen_ai.wildcard.future_thing", "yes"),
            ],
        );
        let inv = OtelGenAiSessionMapper::new(GenAIConventionVersion::Experimental)
            .map(&session(vec![llm]))
            .expect("experimental ignores unknown gen_ai.* keys");
        assert_eq!(inv.model.provider, "openai");
    }
}