poe2-agent 0.5.0

AI agent for Path of Exile 2 build analysis
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
//! Agent eval harness — measures tool selection accuracy, efficiency, and
//! answer correctness across a suite of test cases.
//!
//! Run with:
//!   OPENAI_API_KEY=sk-... cargo test --test eval -- --nocapture
//!
//! Run a single case:
//!   EVAL_CASE=basic_dps OPENAI_API_KEY=sk-... cargo test --test eval -- --nocapture

#![allow(dead_code)]

use std::path::Path;
use std::sync::Arc;
use std::time::{Duration, Instant};

use futures_lite::StreamExt;
use poe2_agent::{AgentEvent, ChatGptClient, PobParser, ToolAgent, Usage};

const POB_PATH: &str = "vendor/PathOfBuilding-PoE2";

// ---------------------------------------------------------------------------
// Data structures
// ---------------------------------------------------------------------------

struct EvalCase {
    name: &'static str,
    question: &'static str,
    expected_tools: &'static [&'static str],
    banned_tools: &'static [&'static str],
    max_tool_rounds: usize,
    answer_must_contain: &'static [&'static str],
    max_total_tokens: Option<usize>,
}

struct Trace {
    tool_calls: Vec<String>,
    tool_result_sizes: Vec<(String, usize)>,
    tool_rounds: usize,
    final_answer: String,
    usage: Usage,
    time_to_first_token: Option<Duration>,
    total_time: Duration,
}

struct CaseResult {
    name: String,
    status: CaseStatus,
    expected_tools_called: Vec<String>,
    expected_tools_missed: Vec<String>,
    banned_tools_called: Vec<String>,
    extra_tools_called: Vec<String>,
    tool_rounds: usize,
    max_tool_rounds: usize,
    rounds_over_budget: bool,
    facts_found: Vec<String>,
    facts_missing: Vec<String>,
    total_tokens: u32,
    tokens_over_budget: bool,
    total_time: Duration,
    time_to_first_token: Option<Duration>,
    all_tools: Vec<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CaseStatus {
    Pass,
    Warn,
    Fail,
}

impl CaseStatus {
    fn icon(self) -> &'static str {
        match self {
            CaseStatus::Pass => "",
            CaseStatus::Warn => "~",
            CaseStatus::Fail => "",
        }
    }
}

// ---------------------------------------------------------------------------
// Fixture
// ---------------------------------------------------------------------------

fn fixture_xml() -> Vec<u8> {
    std::fs::read(Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/ranger-with-gear.xml"))
        .expect("fixture XML missing — run from repo root")
}

// ---------------------------------------------------------------------------
// Trace capture
// ---------------------------------------------------------------------------

async fn capture_trace(agent: &ToolAgent, xml: &[u8], question: &str) -> Trace {
    let start = Instant::now();
    let stream = agent.respond(xml, question, vec![]);
    tokio::pin!(stream);

    let mut tool_calls: Vec<String> = Vec::new();
    let mut tool_result_sizes: Vec<(String, usize)> = Vec::new();
    let mut tool_rounds: usize = 0;
    let mut final_answer = String::new();
    let mut usage = Usage::default();
    let mut time_to_first_token: Option<Duration> = None;
    let mut last_was_tool_call = false;

    while let Some(event) = stream.next().await {
        match event.expect("agent stream error") {
            AgentEvent::ToolCall { name } => {
                if !last_was_tool_call {
                    tool_rounds += 1;
                }
                last_was_tool_call = true;
                tool_calls.push(name);
            }
            AgentEvent::ToolResult { name, size_bytes } => {
                tool_result_sizes.push((name, size_bytes));
            }
            AgentEvent::Token(text) => {
                last_was_tool_call = false;
                if time_to_first_token.is_none() {
                    time_to_first_token = Some(start.elapsed());
                }
                final_answer.push_str(&text);
            }
            AgentEvent::Usage(u) => {
                usage = u;
            }
            _ => {}
        }
    }

    Trace {
        tool_calls,
        tool_result_sizes,
        tool_rounds,
        final_answer,
        usage,
        time_to_first_token,
        total_time: start.elapsed(),
    }
}

// ---------------------------------------------------------------------------
// Scoring
// ---------------------------------------------------------------------------

fn score(case: &EvalCase, trace: &Trace) -> CaseResult {
    let called_set: std::collections::HashSet<&str> =
        trace.tool_calls.iter().map(|s| s.as_str()).collect();
    let expected_set: std::collections::HashSet<&str> =
        case.expected_tools.iter().copied().collect();
    let banned_set: std::collections::HashSet<&str> = case.banned_tools.iter().copied().collect();

    let expected_tools_called: Vec<String> = case
        .expected_tools
        .iter()
        .filter(|t| called_set.contains(**t))
        .map(|t| t.to_string())
        .collect();
    let expected_tools_missed: Vec<String> = case
        .expected_tools
        .iter()
        .filter(|t| !called_set.contains(**t))
        .map(|t| t.to_string())
        .collect();
    let banned_tools_called: Vec<String> = case
        .banned_tools
        .iter()
        .filter(|t| called_set.contains(**t))
        .map(|t| t.to_string())
        .collect();

    // Deduplicate called tools for "extra" calculation
    let unique_called: std::collections::HashSet<&str> = called_set.clone();
    let extra_tools_called: Vec<String> = unique_called
        .iter()
        .filter(|t| !expected_set.contains(**t) && !banned_set.contains(**t))
        .map(|t| t.to_string())
        .collect();

    let rounds_over_budget = trace.tool_rounds > case.max_tool_rounds;

    let answer_lower = trace.final_answer.to_lowercase();
    let facts_found: Vec<String> = case
        .answer_must_contain
        .iter()
        .filter(|f| answer_lower.contains(&f.to_lowercase()))
        .map(|f| f.to_string())
        .collect();
    let facts_missing: Vec<String> = case
        .answer_must_contain
        .iter()
        .filter(|f| !answer_lower.contains(&f.to_lowercase()))
        .map(|f| f.to_string())
        .collect();

    let tokens_over_budget = case
        .max_total_tokens
        .map(|max| trace.usage.total_tokens as usize > max)
        .unwrap_or(false);

    // Determine status
    let has_fail = !expected_tools_missed.is_empty()
        || !banned_tools_called.is_empty()
        || rounds_over_budget
        || !facts_missing.is_empty();

    let has_warn = !extra_tools_called.is_empty() || tokens_over_budget;

    let status = if has_fail {
        CaseStatus::Fail
    } else if has_warn {
        CaseStatus::Warn
    } else {
        CaseStatus::Pass
    };

    CaseResult {
        name: case.name.to_string(),
        status,
        expected_tools_called,
        expected_tools_missed,
        banned_tools_called,
        extra_tools_called,
        tool_rounds: trace.tool_rounds,
        max_tool_rounds: case.max_tool_rounds,
        rounds_over_budget,
        facts_found,
        facts_missing,
        total_tokens: trace.usage.total_tokens,
        tokens_over_budget,
        total_time: trace.total_time,
        time_to_first_token: trace.time_to_first_token,
        all_tools: trace.tool_calls.clone(),
    }
}

// ---------------------------------------------------------------------------
// Eval cases
// ---------------------------------------------------------------------------

fn eval_cases() -> Vec<EvalCase> {
    vec![
        // Category 1: Simple stat lookups
        EvalCase {
            name: "basic_dps",
            question: "What is my total DPS and what main skill am I using?",
            expected_tools: &["get_build_stats", "get_skill_list"],
            banned_tools: &["get_item", "get_passive_tree", "get_jewel", "query_passive_stats"],
            max_tool_rounds: 2,
            answer_must_contain: &["DPS"],
            max_total_tokens: None,
        },
        EvalCase {
            name: "defensive_stats",
            question: "How tanky is this build? What are my defenses?",
            expected_tools: &["get_build_stats"],
            banned_tools: &["get_item", "get_jewel"],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        EvalCase {
            name: "skill_gems",
            question: "What support gems are linked to my main skill?",
            expected_tools: &["get_skill_list"],
            banned_tools: &["get_item", "get_passive_tree", "get_jewel"],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        // Category 2: Gear inspection
        EvalCase {
            name: "specific_item",
            question: "What weapon am I using?",
            expected_tools: &["get_item"],
            banned_tools: &[
                "get_passive_tree",
                "get_jewel",
                "query_passive_stats",
                "get_unallocated_ascendancy",
            ],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        EvalCase {
            name: "missing_gear",
            question: "Am I missing any gear? What slots are empty?",
            expected_tools: &["get_equipped_items"],
            banned_tools: &["get_passive_tree", "get_jewel", "query_passive_stats"],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        EvalCase {
            name: "gear_overview_then_detail",
            question: "What's my worst piece of gear and how could I upgrade it?",
            expected_tools: &["get_equipped_items"],
            banned_tools: &["get_jewel", "query_passive_stats"],
            max_tool_rounds: 2,
            answer_must_contain: &["upgrade"],
            max_total_tokens: None,
        },
        // Category 3: Passive tree
        EvalCase {
            name: "keystones",
            question: "What keystones am I using?",
            expected_tools: &["get_passive_tree"],
            banned_tools: &["get_item", "get_config", "get_jewel"],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        EvalCase {
            name: "jewel_inspection",
            question: "What jewels do I have socketed in my passive tree?",
            expected_tools: &["get_passive_tree", "get_jewel"],
            banned_tools: &["get_item", "get_config", "query_passive_stats"],
            max_tool_rounds: 3,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        EvalCase {
            name: "stat_sourcing",
            question: "How much fire damage am I getting from the passive tree, and is there more nearby?",
            expected_tools: &["query_passive_stats"],
            banned_tools: &["get_item", "get_jewel", "get_config"],
            max_tool_rounds: 2,
            answer_must_contain: &["fire damage"],
            max_total_tokens: None,
        },
        // Category 4: Ascendancy
        EvalCase {
            name: "ascendancy_recommendation",
            question: "What ascendancy nodes should I take next?",
            expected_tools: &["get_unallocated_ascendancy"],
            banned_tools: &["get_item", "get_jewel"],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        EvalCase {
            name: "ascendancy_current",
            question: "What ascendancy am I playing and what nodes do I have?",
            expected_tools: &["get_unallocated_ascendancy"],
            banned_tools: &["get_item", "get_config"],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        // Category 5: Config
        EvalCase {
            name: "build_config",
            question: "What enemy level is my build configured for?",
            expected_tools: &["get_config"],
            banned_tools: &["get_item", "get_passive_tree", "get_jewel"],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        // Category 6: Multi-tool complex questions
        EvalCase {
            name: "full_build_review",
            question: "Give me a quick overview of this build — what's working and what needs improvement?",
            expected_tools: &["get_build_stats"],
            banned_tools: &[],
            max_tool_rounds: 5,
            answer_must_contain: &[],
            max_total_tokens: Some(8000),
        },
        EvalCase {
            name: "upgrade_priorities",
            question: "What are the top 3 things I should upgrade on this build?",
            expected_tools: &["get_build_stats"],
            banned_tools: &[],
            max_tool_rounds: 5,
            answer_must_contain: &[],
            max_total_tokens: Some(10000),
        },
        // Category 7: Edge cases
        EvalCase {
            name: "no_tools_needed",
            question: "What is Path of Exile 2?",
            expected_tools: &[],
            banned_tools: &[],
            max_tool_rounds: 1,
            answer_must_contain: &["Path of Exile"],
            max_total_tokens: None,
        },
        EvalCase {
            name: "ambiguous_item_slot",
            question: "Show me my ring",
            expected_tools: &["get_item"],
            banned_tools: &["get_passive_tree", "get_jewel"],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        // Category 8: New v2 tools
        EvalCase {
            name: "multi_stat_passive",
            question: "How much life, armour, and fire resistance do I get from passives?",
            expected_tools: &["query_passive_stats"],
            banned_tools: &["get_item", "get_jewel", "get_config"],
            max_tool_rounds: 1,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        EvalCase {
            name: "skill_breakdown",
            question: "Why is my main skill's DPS low? Break down the damage.",
            expected_tools: &["get_skill_breakdown"],
            banned_tools: &["get_item", "get_jewel"],
            max_tool_rounds: 3,
            answer_must_contain: &["DPS"],
            max_total_tokens: None,
        },
        EvalCase {
            name: "full_gear_review",
            question: "How's my gear overall? What should I upgrade?",
            expected_tools: &["get_equipped_items"],
            banned_tools: &["get_passive_tree", "get_jewel"],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        // Category 9: Gem search
        EvalCase {
            name: "search_support_gems",
            question: "What support gems work with projectile attacks?",
            expected_tools: &["search_gems"],
            banned_tools: &["get_item", "get_jewel", "get_passive_tree"],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
        EvalCase {
            name: "search_fire_aoe_gems",
            question: "Are there any AoE fire skills I could use?",
            expected_tools: &["search_gems"],
            banned_tools: &["get_item", "get_jewel", "get_passive_tree"],
            max_tool_rounds: 2,
            answer_must_contain: &[],
            max_total_tokens: None,
        },
    ]
}

// ---------------------------------------------------------------------------
// Report
// ---------------------------------------------------------------------------

fn print_report(model: &str, results: &[CaseResult]) {
    let total = results.len();
    let passed = results
        .iter()
        .filter(|r| r.status == CaseStatus::Pass)
        .count();
    let warned = results
        .iter()
        .filter(|r| r.status == CaseStatus::Warn)
        .count();
    let failed = results
        .iter()
        .filter(|r| r.status == CaseStatus::Fail)
        .count();

    eprintln!();
    eprintln!("=== Agent Eval Report (model: {model}) ===");
    eprintln!("  Fixture: ranger-with-gear.xml");
    eprintln!("  Cases:   {total}");
    eprintln!(
        "  Passed:  {passed} ({:.0}%)",
        passed as f64 / total as f64 * 100.0
    );
    eprintln!("  Warned:  {warned}");
    eprintln!("  Failed:  {failed}");
    eprintln!();
    eprintln!("  --- Per-case results ---");
    eprintln!();

    for r in results {
        let round_label = if r.tool_rounds == 1 {
            "round ".to_string()
        } else {
            "rounds".to_string()
        };
        eprintln!(
            "  {} {:<30} {} {}  {:>5} tok  {:.1}s",
            r.status.icon(),
            r.name,
            r.tool_rounds,
            round_label,
            r.total_tokens,
            r.total_time.as_secs_f64(),
        );

        // Tool list
        if !r.all_tools.is_empty() {
            let tool_display: Vec<String> = r
                .all_tools
                .iter()
                .map(|t| {
                    if r.banned_tools_called.contains(t) {
                        format!("{t}(BANNED!)")
                    } else if r.extra_tools_called.contains(t) {
                        format!("{t}(!)")
                    } else {
                        t.clone()
                    }
                })
                .collect();
            eprintln!("    tools: {}", tool_display.join(", "));
        }

        // Failure/warning details
        if !r.expected_tools_missed.is_empty() {
            eprintln!(
                "    FAIL: missing expected tools: {}",
                r.expected_tools_missed.join(", ")
            );
        }
        if !r.banned_tools_called.is_empty() {
            eprintln!(
                "    FAIL: called banned tools: {}",
                r.banned_tools_called.join(", ")
            );
        }
        if r.rounds_over_budget {
            eprintln!(
                "    FAIL: {} rounds > max {}",
                r.tool_rounds, r.max_tool_rounds,
            );
        }
        if !r.facts_missing.is_empty() {
            eprintln!(
                "    FAIL: missing answer facts: {}",
                r.facts_missing.join(", ")
            );
        }
        if !r.extra_tools_called.is_empty() {
            eprintln!("    WARN: extra tools: {}", r.extra_tools_called.join(", "));
        }
        if r.tokens_over_budget {
            eprintln!("    WARN: token budget exceeded ({})", r.total_tokens);
        }
        eprintln!();
    }

    // Aggregate metrics
    let tool_selection_correct = results
        .iter()
        .filter(|r| r.expected_tools_missed.is_empty() && r.banned_tools_called.is_empty())
        .count();
    let no_banned = results
        .iter()
        .filter(|r| r.banned_tools_called.is_empty())
        .count();
    let within_rounds = results.iter().filter(|r| !r.rounds_over_budget).count();
    let facts_correct = results
        .iter()
        .filter(|r| r.facts_missing.is_empty())
        .count();
    let avg_tokens: f64 = results.iter().map(|r| r.total_tokens as f64).sum::<f64>() / total as f64;
    let avg_latency: f64 = results
        .iter()
        .map(|r| r.total_time.as_secs_f64())
        .sum::<f64>()
        / total as f64;
    let unnecessary_rate: f64 = results
        .iter()
        .map(|r| r.extra_tools_called.len() as f64)
        .sum::<f64>()
        / total as f64;

    eprintln!("  --- Aggregate ---");
    eprintln!();
    eprintln!(
        "  Tool selection accuracy: {:>3.0}%  ({}/{} cases all expected tools called, no banned)",
        tool_selection_correct as f64 / total as f64 * 100.0,
        tool_selection_correct,
        total,
    );
    eprintln!(
        "  No-banned-tool rate:    {:>3.0}%  ({}/{} cases called no banned tool)",
        no_banned as f64 / total as f64 * 100.0,
        no_banned,
        total,
    );
    eprintln!(
        "  Efficiency rate:        {:>3.0}%  ({}/{} within round budget)",
        within_rounds as f64 / total as f64 * 100.0,
        within_rounds,
        total,
    );
    eprintln!(
        "  Answer correctness:     {:>3.0}%  ({}/{} all required facts present)",
        facts_correct as f64 / total as f64 * 100.0,
        facts_correct,
        total,
    );
    eprintln!("  Avg total tokens:       {avg_tokens:.0}");
    eprintln!("  Avg latency:            {avg_latency:.1}s");
    eprintln!("  Unnecessary call rate:  {unnecessary_rate:.1} tools/question");
}

// ---------------------------------------------------------------------------
// Test entry point
// ---------------------------------------------------------------------------

#[tokio::test]
async fn eval_suite() {
    let _ = dotenvy::dotenv();

    let api_key = match std::env::var("OPENAI_API_KEY") {
        Ok(k) if !k.is_empty() => k,
        _ => {
            eprintln!("\n=== Skipping eval suite (no OPENAI_API_KEY) ===");
            return;
        }
    };

    let model = std::env::var("OPENAI_MODEL").unwrap_or_else(|_| "gpt-4.1-nano".into());
    let case_filter = std::env::var("EVAL_CASE").ok();

    eprintln!("\n=== Agent Eval Suite (model: {model}) ===");
    if let Some(ref filter) = case_filter {
        eprintln!("  Filter: {filter}");
    }

    // Init shared resources
    let parser = Arc::new(
        PobParser::new(Path::new(POB_PATH))
            .await
            .expect("PobParser::new failed"),
    );
    let llm = ChatGptClient::new(&api_key, &model).expect("ChatGptClient::new failed");
    let agent = ToolAgent::new(llm.clone(), parser, None);
    let xml = fixture_xml();

    let cases = eval_cases();
    let cases: Vec<&EvalCase> = if let Some(ref filter) = case_filter {
        cases
            .iter()
            .filter(|c| c.name.contains(filter.as_str()))
            .collect()
    } else {
        cases.iter().collect()
    };

    if cases.is_empty() {
        eprintln!("  No cases match filter — exiting.");
        return;
    }

    let mut results: Vec<CaseResult> = Vec::with_capacity(cases.len());

    for case in &cases {
        eprintln!("  Running: {} ...", case.name);
        let trace = capture_trace(&agent, &xml, case.question).await;
        let result = score(case, &trace);
        eprintln!(
            "    {} {} ({} rounds, {} tokens, {:.1}s)",
            result.status.icon(),
            result.name,
            result.tool_rounds,
            result.total_tokens,
            result.total_time.as_secs_f64(),
        );
        results.push(result);
    }

    print_report(llm.model(), &results);

    // Uncomment to fail CI on any failure:
    // let failures = results.iter().filter(|r| r.status == CaseStatus::Fail).count();
    // assert_eq!(failures, 0, "{failures} eval case(s) failed");
}