scouter-evaluate 0.25.0

LLM Evaluation logic for Scouter
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
use crate::error::EvaluationError;
use crate::tasks::evaluator::{PATH_REGEX, REGEX_FIELD_PARSE_PATTERN};
use potato_head::{ChatResponse, Provider};
use scouter_types::genai::AgentAssertion;
use serde_json::{json, Value};
use tracing::error;

const MAX_PATH_LEN: usize = 512;
const MAX_PATH_SEGMENTS: usize = 32;

/// Builds evaluation context from vendor LLM request/response data.
/// Normalizes vendor-specific formats into a standard structure for assertion evaluation.
#[derive(Debug, Clone)]
pub struct AgentContextBuilder {
    response: ChatResponse,
    raw: Value,
}

impl AgentContextBuilder {
    /// Build an AgentContextBuilder from raw context JSON.
    ///
    /// Extraction strategy (priority order):
    /// 1. Pre-normalized — if top-level keys match our standard shape
    /// 2. OpenAI format — choices[].message.tool_calls, usage, model
    /// 3. Anthropic format — content[] with ToolUseBlock, usage, model
    /// 4. Google/Gemini format — candidates[].content.parts[] with function_call
    /// 5. Fallback — walk JSON tree for known patterns
    pub fn from_context(
        context: &Value,
        provider: Option<&Provider>,
    ) -> Result<Self, EvaluationError> {
        let response_val = context.get("response").unwrap_or(context);
        let response =
            ChatResponse::from_response_value(response_val.clone(), provider).map_err(|e| {
                error!("Failed to parse response: {}", e);
                EvaluationError::InvalidProviderResponse
            })?;
        Ok(Self {
            response,
            raw: response_val.clone(),
        })
    }

    /// Resolve an AgentAssertion variant to a JSON value for the shared AssertionEvaluator.
    pub fn build_context(&self, assertion: &AgentAssertion) -> Result<Value, EvaluationError> {
        match assertion {
            AgentAssertion::ToolCalled { name } => {
                let found = self
                    .response
                    .get_tool_calls()
                    .iter()
                    .any(|tc| tc.name == *name);
                Ok(json!(found))
            }
            AgentAssertion::ToolNotCalled { name } => {
                let not_found = !self
                    .response
                    .get_tool_calls()
                    .iter()
                    .any(|tc| tc.name == *name);
                Ok(json!(not_found))
            }
            AgentAssertion::ToolCalledWithArgs { name, arguments } => {
                let matched =
                    self.response.get_tool_calls().iter().any(|tc| {
                        tc.name == *name && Self::partial_match(&tc.arguments, &arguments.0)
                    });
                Ok(json!(matched))
            }
            AgentAssertion::ToolCallSequence { names } => {
                let actual: Vec<String> = self
                    .response
                    .get_tool_calls()
                    .iter()
                    .map(|tc| tc.name.clone())
                    .collect();
                let mut expected_iter = names.iter();
                let mut current = expected_iter.next();
                for actual_name in &actual {
                    if let Some(exp) = current {
                        if actual_name == exp {
                            current = expected_iter.next();
                        }
                    }
                }
                Ok(json!(current.is_none()))
            }
            AgentAssertion::ToolCallCount { name } => {
                let tools = &self.response.get_tool_calls();
                let count = if let Some(name) = name {
                    tools.iter().filter(|tc| tc.name == *name).count()
                } else {
                    tools.len()
                };
                Ok(json!(count))
            }
            AgentAssertion::ToolArgument { name, argument_key } => {
                let value = self
                    .response
                    .get_tool_calls()
                    .iter()
                    .find(|tc| tc.name == *name)
                    .and_then(|tc| tc.arguments.get(argument_key))
                    .cloned()
                    .unwrap_or(Value::Null);

                Ok(value)
            }
            AgentAssertion::ToolResult { name } => {
                let value = self
                    .response
                    .get_tool_calls()
                    .iter()
                    .find(|tc| tc.name == *name)
                    .and_then(|tc| tc.result.clone())
                    .unwrap_or(Value::Null);

                Ok(value)
            }
            AgentAssertion::ResponseContent {} => {
                let text = self.response.response_text();
                if text.is_empty() {
                    Ok(Value::Null)
                } else {
                    Ok(json!(text))
                }
            }
            AgentAssertion::ResponseModel {} => Ok(self
                .response
                .model_name()
                .map(|m| json!(m))
                .unwrap_or(Value::Null)),
            AgentAssertion::ResponseFinishReason {} => Ok(self
                .response
                .finish_reason_str()
                .map(|f| json!(f))
                .unwrap_or(Value::Null)),
            AgentAssertion::ResponseInputTokens {} => Ok(self
                .response
                .input_tokens()
                .map(|t| json!(t))
                .unwrap_or(Value::Null)),
            AgentAssertion::ResponseOutputTokens {} => Ok(self
                .response
                .output_tokens()
                .map(|t| json!(t))
                .unwrap_or(Value::Null)),
            AgentAssertion::ResponseTotalTokens {} => Ok(self
                .response
                .total_tokens()
                .map(|t| json!(t))
                .unwrap_or(Value::Null)),
            AgentAssertion::ResponseField { path } => Self::extract_by_path(&self.raw, path),
        }
    }

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

    /// Check if all specified args are present and equal in actual args (partial match).
    fn partial_match(actual: &Value, expected: &Value) -> bool {
        match (actual, expected) {
            (Value::Object(actual_map), Value::Object(expected_map)) => {
                for (key, expected_val) in expected_map {
                    match actual_map.get(key) {
                        Some(actual_val) => {
                            if !Self::partial_match(actual_val, expected_val) {
                                return false;
                            }
                        }
                        None => return false,
                    }
                }
                true
            }
            _ => actual == expected,
        }
    }

    /// Extract a value from JSON using dot-notation path with array indexing.
    /// Supports: "foo.bar", "foo[0].bar", "candidates[0].content.parts[0].text"
    fn extract_by_path(val: &Value, path: &str) -> Result<Value, EvaluationError> {
        let mut current = val.clone();

        for segment in Self::parse_path_segments(path)? {
            match segment {
                PathSegment::Key(key) => {
                    current = current.get(&key).cloned().unwrap_or(Value::Null);
                }
                PathSegment::Index(idx) => {
                    current = current
                        .as_array()
                        .and_then(|arr| arr.get(idx))
                        .cloned()
                        .unwrap_or(Value::Null);
                }
            }
        }

        Ok(current)
    }

    fn parse_path_segments(path: &str) -> Result<Vec<PathSegment>, EvaluationError> {
        if path.len() > MAX_PATH_LEN {
            return Err(EvaluationError::PathTooLong(path.len()));
        }

        let regex = PATH_REGEX.get_or_init(|| {
            regex::Regex::new(REGEX_FIELD_PARSE_PATTERN)
                .expect("Invalid regex pattern in REGEX_FIELD_PARSE_PATTERN")
        });

        let mut segments = Vec::new();

        for capture in regex.find_iter(path) {
            let s = capture.as_str();
            if s.starts_with('[') && s.ends_with(']') {
                let idx_str = &s[1..s.len() - 1];
                let idx = idx_str
                    .parse::<usize>()
                    .map_err(|_| EvaluationError::InvalidArrayIndex(idx_str.to_string()))?;
                segments.push(PathSegment::Index(idx));
            } else {
                segments.push(PathSegment::Key(s.to_string()));
            }
        }

        if segments.is_empty() {
            return Err(EvaluationError::EmptyFieldPath);
        }

        if segments.len() > MAX_PATH_SEGMENTS {
            return Err(EvaluationError::TooManyPathSegments(segments.len()));
        }

        Ok(segments)
    }
}

enum PathSegment {
    Key(String),
    Index(usize),
}

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

    #[test]
    fn test_tool_called_assertion() {
        let context = json!({
            "model": "gpt-4o",
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": null,
                    "tool_calls": [{
                        "id": "call_1",
                        "type": "function",
                        "function": {"name": "web_search", "arguments": "{\"query\": \"test\"}"}
                    }]
                },
                "finish_reason": "tool_calls"
            }]
        });

        let builder = AgentContextBuilder::from_context(&context, None).unwrap();

        let result = builder
            .build_context(&AgentAssertion::ToolCalled {
                name: "web_search".to_string(),
            })
            .unwrap();
        assert_eq!(result, json!(true));

        let result = builder
            .build_context(&AgentAssertion::ToolNotCalled {
                name: "delete_user".to_string(),
            })
            .unwrap();
        assert_eq!(result, json!(true));

        let result = builder
            .build_context(&AgentAssertion::ToolCallCount { name: None })
            .unwrap();
        assert_eq!(result, json!(1));
    }

    #[test]
    fn test_tool_called_with_args_partial_match() {
        let context = json!({
            "model": "gpt-4o",
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": null,
                    "tool_calls": [{
                        "id": "call_1",
                        "type": "function",
                        "function": {"name": "web_search", "arguments": "{\"query\": \"weather NYC\", \"lang\": \"en\", \"limit\": 5}"}
                    }]
                },
                "finish_reason": "tool_calls"
            }]
        });

        let builder = AgentContextBuilder::from_context(&context, None).unwrap();

        // Partial match - only checking "query"
        let result = builder
            .build_context(&AgentAssertion::ToolCalledWithArgs {
                name: "web_search".to_string(),
                arguments: PyValueWrapper(json!({"query": "weather NYC"})),
            })
            .unwrap();
        assert_eq!(result, json!(true));

        // Non-matching arg
        let result = builder
            .build_context(&AgentAssertion::ToolCalledWithArgs {
                name: "web_search".to_string(),
                arguments: PyValueWrapper(json!({"query": "weather LA"})),
            })
            .unwrap();
        assert_eq!(result, json!(false));
    }

    #[test]
    fn test_tool_call_sequence() {
        let context = json!({
            "model": "gpt-4o",
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": null,
                    "tool_calls": [
                        {"id": "call_1", "type": "function", "function": {"name": "web_search", "arguments": "{}"}},
                        {"id": "call_2", "type": "function", "function": {"name": "summarize", "arguments": "{}"}},
                        {"id": "call_3", "type": "function", "function": {"name": "respond", "arguments": "{}"}}
                    ]
                },
                "finish_reason": "tool_calls"
            }]
        });

        let builder = AgentContextBuilder::from_context(&context, None).unwrap();

        let result = builder
            .build_context(&AgentAssertion::ToolCallSequence {
                names: vec![
                    "web_search".to_string(),
                    "summarize".to_string(),
                    "respond".to_string(),
                ],
            })
            .unwrap();
        assert_eq!(result, json!(true));

        // Wrong order
        let result = builder
            .build_context(&AgentAssertion::ToolCallSequence {
                names: vec!["respond".to_string(), "web_search".to_string()],
            })
            .unwrap();
        assert_eq!(result, json!(false));
    }

    #[test]
    fn test_response_field_escape_hatch() {
        let context = json!({
            "response": {
                "candidates": [{
                    "content": {"role": "model", "parts": [{"text": "hello"}]},
                    "finishReason": "STOP",
                    "safety_ratings": [{"category": "HARM_CATEGORY_SAFE"}]
                }],
                "usageMetadata": {"promptTokenCount": 5, "candidatesTokenCount": 2}
            }
        });

        let builder = AgentContextBuilder::from_context(&context, None).unwrap();

        // Path is relative to response_val (the candidates object), not the full context
        let result = builder
            .build_context(&AgentAssertion::ResponseField {
                path: "candidates[0].safety_ratings[0].category".to_string(),
            })
            .unwrap();
        assert_eq!(result, json!("HARM_CATEGORY_SAFE"));
    }

    #[test]
    fn test_no_tool_calls() {
        let context = json!({
            "model": "gpt-4o",
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": "Just a text response."
                },
                "finish_reason": "stop"
            }]
        });

        let builder = AgentContextBuilder::from_context(&context, None).unwrap();

        let result = builder
            .build_context(&AgentAssertion::ToolNotCalled {
                name: "web_search".to_string(),
            })
            .unwrap();
        assert_eq!(result, json!(true));
    }

    #[test]
    fn test_from_context_invalid_json() {
        // Empty object has no recognizable vendor keys
        let context = json!({});
        let result = AgentContextBuilder::from_context(&context, None);
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(EvaluationError::InvalidProviderResponse)
        ));
    }

    #[test]
    fn test_tool_call_sequence_subsequence() {
        let context = json!({
            "model": "gpt-4o",
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": null,
                    "tool_calls": [
                        {"id": "c1", "type": "function", "function": {"name": "search", "arguments": "{}"}},
                        {"id": "c2", "type": "function", "function": {"name": "filter", "arguments": "{}"}},
                        {"id": "c3", "type": "function", "function": {"name": "rank", "arguments": "{}"}},
                        {"id": "c4", "type": "function", "function": {"name": "respond", "arguments": "{}"}}
                    ]
                },
                "finish_reason": "tool_calls"
            }]
        });

        let builder = AgentContextBuilder::from_context(&context, None).unwrap();

        // Non-contiguous in-order subsequence should pass
        let result = builder
            .build_context(&AgentAssertion::ToolCallSequence {
                names: vec![
                    "search".to_string(),
                    "rank".to_string(),
                    "respond".to_string(),
                ],
            })
            .unwrap();
        assert_eq!(result, json!(true));

        // Out-of-order should fail
        let result = builder
            .build_context(&AgentAssertion::ToolCallSequence {
                names: vec!["respond".to_string(), "search".to_string()],
            })
            .unwrap();
        assert_eq!(result, json!(false));
    }

    #[test]
    fn test_parse_path_segments_errors() {
        // Empty string -> EmptyFieldPath
        let result = AgentContextBuilder::parse_path_segments("");
        assert!(matches!(result, Err(EvaluationError::EmptyFieldPath)));

        // Path exceeding max length
        let long_path = "a".repeat(MAX_PATH_LEN + 1);
        let result = AgentContextBuilder::parse_path_segments(&long_path);
        assert!(matches!(result, Err(EvaluationError::PathTooLong(_))));

        // Too many segments
        let many_segments = (0..MAX_PATH_SEGMENTS + 1)
            .map(|i| format!("seg{}", i))
            .collect::<Vec<_>>()
            .join(".");
        let result = AgentContextBuilder::parse_path_segments(&many_segments);
        assert!(matches!(
            result,
            Err(EvaluationError::TooManyPathSegments(_))
        ));
    }

    #[test]
    fn test_response_content_empty() {
        let context = json!({
            "model": "gpt-4o",
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": null
                },
                "finish_reason": "stop"
            }]
        });

        let builder = AgentContextBuilder::from_context(&context, None).unwrap();
        let result = builder
            .build_context(&AgentAssertion::ResponseContent {})
            .unwrap();
        assert_eq!(result, Value::Null);
    }

    #[test]
    fn test_partial_match_nested() {
        let context = json!({
            "model": "gpt-4o",
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": null,
                    "tool_calls": [{
                        "id": "c1",
                        "type": "function",
                        "function": {"name": "create_item", "arguments": "{\"item\": {\"name\": \"widget\", \"price\": 9.99, \"tags\": [\"sale\"]}}"}
                    }]
                },
                "finish_reason": "tool_calls"
            }]
        });

        let builder = AgentContextBuilder::from_context(&context, None).unwrap();

        // Nested partial match - only check inner "name"
        let result = builder
            .build_context(&AgentAssertion::ToolCalledWithArgs {
                name: "create_item".to_string(),
                arguments: PyValueWrapper(json!({"item": {"name": "widget"}})),
            })
            .unwrap();
        assert_eq!(result, json!(true));

        // Nested mismatch
        let result = builder
            .build_context(&AgentAssertion::ToolCalledWithArgs {
                name: "create_item".to_string(),
                arguments: PyValueWrapper(json!({"item": {"name": "gadget"}})),
            })
            .unwrap();
        assert_eq!(result, json!(false));
    }

    #[test]
    fn test_tool_result_extraction() {
        // tool result values are not standard in OpenAI format; test ToolResult returns Null
        // when no result is present (tool_calls don't carry result in request JSON)
        let context = json!({
            "model": "gpt-4o",
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": null,
                    "tool_calls": [{
                        "id": "c1",
                        "type": "function",
                        "function": {"name": "web_search", "arguments": "{\"query\": \"test\"}"}
                    }]
                },
                "finish_reason": "tool_calls"
            }]
        });

        let builder = AgentContextBuilder::from_context(&context, None).unwrap();

        // Named tool with no result -> Null
        let result = builder
            .build_context(&AgentAssertion::ToolResult {
                name: "web_search".to_string(),
            })
            .unwrap();
        assert_eq!(result, Value::Null);

        // Missing tool name -> Null
        let result = builder
            .build_context(&AgentAssertion::ToolResult {
                name: "nonexistent".to_string(),
            })
            .unwrap();
        assert_eq!(result, Value::Null);
    }

    #[test]
    fn test_tool_argument_extraction() {
        let context = json!({
            "model": "gpt-4o",
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": null,
                    "tool_calls": [{
                        "id": "call_1",
                        "type": "function",
                        "function": {"name": "web_search", "arguments": "{\"query\": \"test query\", \"limit\": 10}"}
                    }]
                },
                "finish_reason": "tool_calls"
            }]
        });

        let builder = AgentContextBuilder::from_context(&context, None).unwrap();

        let result = builder
            .build_context(&AgentAssertion::ToolArgument {
                name: "web_search".to_string(),
                argument_key: "query".to_string(),
            })
            .unwrap();
        assert_eq!(result, json!("test query"));

        let result = builder
            .build_context(&AgentAssertion::ToolArgument {
                name: "web_search".to_string(),
                argument_key: "missing".to_string(),
            })
            .unwrap();
        assert_eq!(result, Value::Null);
    }
}