prompty 2.0.0-beta.1

Prompty is an asset class and format for LLM prompts
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
//! Agent vector tests — exercises the agent loop (`turn()`) using canned
//! LLM responses from `spec/vectors/agent/agent_vectors.json`.
//!
//! Each vector provides a `sequence` of mock LLM responses and the expected
//! final result (or error). A `MockExecutor` replays the canned responses;
//! a `MockProcessor` extracts content or tool calls from the response.
//! Tool functions return canned results from the vector's `tool_results`.

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use serde_json::{Value, json};

use prompty::interfaces::{Executor, InvokerError, Processor};
use prompty::model::Prompty;
use prompty::model::context::LoadContext;
use prompty::pipeline::{AgentEvent, EventCallback, ToolHandler, TurnOptions, turn};
use prompty::types::{Message, Role};

// ---------------------------------------------------------------------------
// Helpers — load vectors JSON
// ---------------------------------------------------------------------------

fn vectors_path() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap() // runtime/rust/
        .parent()
        .unwrap() // runtime/
        .parent()
        .unwrap() // repo root
        .join("spec")
        .join("vectors")
        .join("agent")
        .join("agent_vectors.json")
}

fn load_vectors() -> Vec<Value> {
    let content =
        std::fs::read_to_string(vectors_path()).expect("failed to read agent_vectors.json");
    serde_json::from_str(&content).expect("failed to parse agent_vectors.json")
}

fn find_vector(name: &str) -> Value {
    load_vectors()
        .into_iter()
        .find(|v| v["name"].as_str() == Some(name))
        .unwrap_or_else(|| panic!("vector '{name}' not found"))
}

// ---------------------------------------------------------------------------
// MockExecutor — replays canned LLM responses from the vector sequence
// ---------------------------------------------------------------------------

struct MockExecutor {
    responses: Vec<Value>,
    call_idx: AtomicUsize,
}

impl MockExecutor {
    fn new(responses: Vec<Value>) -> Self {
        Self {
            responses,
            call_idx: AtomicUsize::new(0),
        }
    }
}

#[async_trait]
impl Executor for MockExecutor {
    async fn execute(
        &self,
        _agent: &Prompty,
        _messages: &[Message],
    ) -> Result<Value, InvokerError> {
        let idx = self.call_idx.fetch_add(1, Ordering::SeqCst);
        if idx >= self.responses.len() {
            return Err(InvokerError::Execute(
                format!("MockExecutor: no more responses (requested index {idx})").into(),
            ));
        }
        Ok(self.responses[idx].clone())
    }

    // Use default format_tool_messages (OpenAI style) — inherited from trait
}

// ---------------------------------------------------------------------------
// MockProcessor — extracts content or tool calls from a canned response
// ---------------------------------------------------------------------------

struct MockProcessor;

#[async_trait]
impl Processor for MockProcessor {
    async fn process(&self, _agent: &Prompty, response: Value) -> Result<Value, InvokerError> {
        // Navigate OpenAI-style response: choices[0].message
        let message = &response["choices"][0]["message"];

        // Check for tool_calls
        if let Some(tool_calls) = message.get("tool_calls") {
            if tool_calls.is_array() && !tool_calls.as_array().unwrap().is_empty() {
                // Return array of {id, name, arguments} for the pipeline to extract
                let calls: Vec<Value> = tool_calls
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|tc| {
                        json!({
                            "id": tc["id"],
                            "name": tc["function"]["name"],
                            "arguments": tc["function"]["arguments"],
                        })
                    })
                    .collect();
                return Ok(Value::Array(calls));
            }
        }

        // No tool calls — return content string
        let content = message
            .get("content")
            .and_then(|c| c.as_str())
            .unwrap_or("");
        Ok(Value::String(content.to_string()))
    }
}

// ---------------------------------------------------------------------------
// Helper — register mocks under a unique key (tests run in parallel, using
// a static global registry). We use a per-test key to avoid collisions.
// ---------------------------------------------------------------------------

fn register_mocks(key: &str, responses: Vec<Value>) {
    prompty::register_executor(key, MockExecutor::new(responses));
    prompty::register_processor(key, MockProcessor);
}

// ---------------------------------------------------------------------------
// Helper — build a Prompty agent from a vector's input section
// ---------------------------------------------------------------------------

fn build_agent(vector: &Value, provider_key: &str) -> Prompty {
    let tools = vector["input"]["tools"].clone();
    let data = json!({
        "name": format!("agent_test_{}", vector["name"].as_str().unwrap_or("unknown")),
        "kind": "prompt",
        "model": {
            "id": "gpt-4",
            "provider": provider_key,
        },
        "instructions": "system:\nYou are a test agent.\n\nuser:\n{{ question }}",
        "tools": tools,
        "template": {
            "format": { "kind": "nunjucks" },
            "parser": { "kind": "prompty" }
        }
    });
    Prompty::load_from_value(&data, &LoadContext::default())
}

// ---------------------------------------------------------------------------
// Helper — collect LLM responses from the vector's sequence
// ---------------------------------------------------------------------------

fn collect_responses(vector: &Value) -> Vec<Value> {
    vector["sequence"]
        .as_array()
        .unwrap_or(&vec![])
        .iter()
        .map(|step| step["llm_response"].clone())
        .collect()
}

// ---------------------------------------------------------------------------
// Helper — build tool handlers that return canned results
// ---------------------------------------------------------------------------

fn build_tool_handlers(vector: &Value) -> HashMap<String, ToolHandler> {
    // Collect all tool results across the sequence, keyed by tool name
    let mut result_queues: HashMap<String, Vec<String>> = HashMap::new();

    if let Some(sequence) = vector["sequence"].as_array() {
        for step in sequence {
            if let Some(tool_results) = step["tool_results"].as_array() {
                // Find the expected_tool_calls to map tool_call_id → tool name
                let expected_calls = step["expected_tool_calls"].as_array();

                for tr in tool_results {
                    let tool_call_id = tr["tool_call_id"].as_str().unwrap_or("");
                    let result = tr["result"].as_str().unwrap_or("").to_string();

                    // Find the tool name from expected_tool_calls
                    let tool_name = expected_calls
                        .and_then(|calls| {
                            calls
                                .iter()
                                .find(|c| c["id"].as_str() == Some(tool_call_id))
                        })
                        .and_then(|c| c["name"].as_str())
                        .unwrap_or("unknown")
                        .to_string();

                    result_queues.entry(tool_name).or_default().push(result);
                }
            }
        }
    }

    // Also collect tool names from tool_functions that may not appear in results
    if let Some(tf) = vector["input"]["tool_functions"].as_object() {
        for name in tf.keys() {
            result_queues.entry(name.clone()).or_default();
        }
    }

    // Build handlers
    let mut handlers: HashMap<String, ToolHandler> = HashMap::new();
    for (name, queue) in result_queues {
        let queue = Arc::new(queue);
        let idx = Arc::new(AtomicUsize::new(0));
        let name_clone = name.clone();
        handlers.insert(
            name,
            ToolHandler::Sync(Box::new(move |_args: Value| {
                let i = idx.fetch_add(1, Ordering::SeqCst);
                if i < queue.len() {
                    Ok(queue[i].clone())
                } else {
                    Ok(format!("(mock result #{i} for {name_clone})"))
                }
            })),
        );
    }

    handlers
}

// ---------------------------------------------------------------------------
// Helper — build the initial messages from the vector's input
// ---------------------------------------------------------------------------

fn build_messages(vector: &Value) -> Vec<Message> {
    vector["input"]["messages"]
        .as_array()
        .unwrap_or(&vec![])
        .iter()
        .filter_map(|m| {
            let role_str = m["role"].as_str()?;
            let role = Role::from_str_opt(role_str)?;
            let content = m.get("content").and_then(|c| c.as_str()).unwrap_or("");
            Some(Message::with_text(role, content))
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Helper — unique mock key per test to avoid registry collisions
// ---------------------------------------------------------------------------

fn mock_key(vector_name: &str) -> String {
    format!("specmock_{vector_name}")
}

// ---------------------------------------------------------------------------
// Core run helper — runs a single agent vector through `turn()`
// ---------------------------------------------------------------------------

async fn run_vector(vector_name: &str) -> Result<Value, InvokerError> {
    let vector = find_vector(vector_name);
    let key = mock_key(vector_name);
    let responses = collect_responses(&vector);
    register_mocks(&key, responses);

    let agent = build_agent(&vector, &key);
    let tools = build_tool_handlers(&vector);
    let _messages = build_messages(&vector);

    // Build agent input — we bypass `prepare` and drive `turn` directly.
    // Since `turn` calls `prepare` internally, we instead use a simpler
    // approach: call the executor/processor loop manually via turn() with
    // pre-built messages passed through a metadata trick.
    //
    // Actually, `turn()` takes `agent + inputs` and calls `prepare()` internally.
    // We need to provide the messages directly. Looking at the pipeline, the
    // simplest approach is to set `agent.instructions` so that `prepare()`
    // produces the messages we want, OR we can just test the agent loop
    // behavior by working at a slightly higher level.
    //
    // The cleanest approach: set instructions to produce the exact input
    // messages, then let turn() handle the loop.

    // Build an agent whose instructions will produce the right messages
    let mut agent = agent;
    let input_msgs = &vector["input"]["messages"];
    let mut instruction_lines = Vec::new();
    if let Some(msgs) = input_msgs.as_array() {
        for m in msgs {
            let role = m["role"].as_str().unwrap_or("user");
            let content = m["content"].as_str().unwrap_or("");
            instruction_lines.push(format!("{role}:\n{content}"));
        }
    }
    agent.instructions = Some(instruction_lines.join("\n\n"));

    let opts = TurnOptions {
        tools,
        ..Default::default()
    };

    // Register the default nunjucks renderer + prompty parser
    prompty::pipeline::register_defaults();

    turn(&agent, None, Some(opts)).await
}

// ===================================================================
// BASIC AGENT LOOP VECTORS
// ===================================================================

#[tokio::test]
async fn test_no_tool_calls() {
    let result = run_vector("no_tool_calls").await.unwrap();
    assert_eq!(result.as_str().unwrap(), "2 + 2 equals 4.");
}

#[tokio::test]
async fn test_single_tool_call() {
    let result = run_vector("single_tool_call").await.unwrap();
    let vector = find_vector("single_tool_call");
    let expected = vector["expected"]["result"].as_str().unwrap();
    assert_eq!(result.as_str().unwrap(), expected);
}

#[tokio::test]
async fn test_multiple_tool_calls_single_turn() {
    let result = run_vector("multiple_tool_calls_single_turn").await.unwrap();
    let vector = find_vector("multiple_tool_calls_single_turn");
    let expected = vector["expected"]["result"].as_str().unwrap();
    assert_eq!(result.as_str().unwrap(), expected);
}

#[tokio::test]
async fn test_multi_turn_tool_calls() {
    let result = run_vector("multi_turn_tool_calls").await.unwrap();
    let vector = find_vector("multi_turn_tool_calls");
    let expected = vector["expected"]["result"].as_str().unwrap();
    assert_eq!(result.as_str().unwrap(), expected);
}

#[tokio::test]
async fn test_tool_result_message_format() {
    let result = run_vector("tool_result_message_format").await.unwrap();
    let vector = find_vector("tool_result_message_format");
    let expected = vector["expected"]["result"].as_str().unwrap();
    assert_eq!(result.as_str().unwrap(), expected);
}

#[tokio::test]
async fn test_assistant_tool_calls_metadata() {
    let result = run_vector("assistant_tool_calls_metadata").await.unwrap();
    let vector = find_vector("assistant_tool_calls_metadata");
    let expected = vector["expected"]["result"].as_str().unwrap();
    assert_eq!(result.as_str().unwrap(), expected);
}

#[tokio::test]
async fn test_empty_tool_result() {
    let result = run_vector("empty_tool_result").await.unwrap();
    let vector = find_vector("empty_tool_result");
    let expected = vector["expected"]["result"].as_str().unwrap();
    assert_eq!(result.as_str().unwrap(), expected);
}

// ===================================================================
// ERROR CASES
// ===================================================================

#[tokio::test]
async fn test_tool_not_registered_error() {
    // The LLM tries to call "unknown_tool" which has no handler.
    // Missing tool is non-fatal (matching TypeScript) — error string sent to LLM.
    let vector = find_vector("tool_not_registered_error");
    let key = mock_key("tool_not_registered_error");

    // Need an extra response: after the missing-tool error string is sent back
    // to the LLM, the mock needs to return a final non-tool-call response.
    let mut responses = collect_responses(&vector);
    responses.push(json!({
        "choices": [{
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "I could not find that tool."
            },
            "finish_reason": "stop"
        }]
    }));
    register_mocks(&key, responses);

    let mut agent = build_agent(&vector, &key);
    let input_msgs = &vector["input"]["messages"];
    let mut instruction_lines = Vec::new();
    if let Some(msgs) = input_msgs.as_array() {
        for m in msgs {
            let role = m["role"].as_str().unwrap_or("user");
            let content = m["content"].as_str().unwrap_or("");
            instruction_lines.push(format!("{role}:\n{content}"));
        }
    }
    agent.instructions = Some(instruction_lines.join("\n\n"));

    // Only register get_weather — NOT unknown_tool
    let mut tools: HashMap<String, ToolHandler> = HashMap::new();
    tools.insert(
        "get_weather".to_string(),
        ToolHandler::Sync(Box::new(|_| Ok("72°F".to_string()))),
    );

    let opts = TurnOptions {
        tools,
        ..Default::default()
    };

    prompty::pipeline::register_defaults();

    let result = turn(&agent, None, Some(opts)).await.unwrap();
    assert!(result.is_string());
}

#[tokio::test]
async fn test_max_iterations_exceeded() {
    // The LLM returns tool calls on every turn for 11 turns.
    // With max_iterations=10, the loop should error (matching TypeScript).
    let vector = find_vector("max_iterations_exceeded");
    let key = mock_key("max_iterations_exceeded");
    let responses = collect_responses(&vector);
    register_mocks(&key, responses);

    let mut agent = build_agent(&vector, &key);
    let input_msgs = &vector["input"]["messages"];
    let mut instruction_lines = Vec::new();
    if let Some(msgs) = input_msgs.as_array() {
        for m in msgs {
            let role = m["role"].as_str().unwrap_or("user");
            let content = m["content"].as_str().unwrap_or("");
            instruction_lines.push(format!("{role}:\n{content}"));
        }
    }
    agent.instructions = Some(instruction_lines.join("\n\n"));

    let tools = build_tool_handlers(&vector);

    let opts = TurnOptions {
        tools,
        max_iterations: 10,
        ..Default::default()
    };

    prompty::pipeline::register_defaults();

    // Max iterations exceeded → error (matching TypeScript behavior)
    let result = turn(&agent, None, Some(opts)).await;
    assert!(result.is_err(), "Expected iteration error, got: {result:?}");
    let err = result.unwrap_err();
    assert!(
        err.to_string().contains("max iterations") || err.to_string().contains("exceeded"),
        "Expected max iterations error message, got: {err}"
    );
}

// ===================================================================
// ASYNC TOOL FUNCTION
// ===================================================================

#[tokio::test]
async fn test_async_tool_function() {
    let vector = find_vector("async_tool_function");
    let key = mock_key("async_tool_function");
    let responses = collect_responses(&vector);
    register_mocks(&key, responses);

    let mut agent = build_agent(&vector, &key);
    let input_msgs = &vector["input"]["messages"];
    let mut instruction_lines = Vec::new();
    if let Some(msgs) = input_msgs.as_array() {
        for m in msgs {
            let role = m["role"].as_str().unwrap_or("user");
            let content = m["content"].as_str().unwrap_or("");
            instruction_lines.push(format!("{role}:\n{content}"));
        }
    }
    agent.instructions = Some(instruction_lines.join("\n\n"));

    let mut tools: HashMap<String, ToolHandler> = HashMap::new();
    tools.insert(
        "lookup".to_string(),
        ToolHandler::Async(Box::new(|_args| {
            Box::pin(async move { Ok("found: test data".to_string()) })
        })),
    );

    let opts = TurnOptions {
        tools,
        ..Default::default()
    };

    prompty::pipeline::register_defaults();

    let result = turn(&agent, None, Some(opts)).await.unwrap();
    assert_eq!(result.as_str().unwrap(), "I found: test data");
}

// ===================================================================
// EVENT VECTORS
// ===================================================================

/// Helper to run a vector with event collection.
async fn run_vector_with_events(
    vector_name: &str,
    tool_override: Option<HashMap<String, ToolHandler>>,
    cancelled: Option<Arc<AtomicBool>>,
) -> (Result<Value, InvokerError>, Vec<String>) {
    let vector = find_vector(vector_name);
    let key = mock_key(vector_name);
    let responses = collect_responses(&vector);
    register_mocks(&key, responses);

    let mut agent = build_agent(&vector, &key);
    let input_msgs = &vector["input"]["messages"];
    let mut instruction_lines = Vec::new();
    if let Some(msgs) = input_msgs.as_array() {
        for m in msgs {
            let role = m["role"].as_str().unwrap_or("user");
            let content = m["content"].as_str().unwrap_or("");
            instruction_lines.push(format!("{role}:\n{content}"));
        }
    }
    agent.instructions = Some(instruction_lines.join("\n\n"));

    let tools = tool_override.unwrap_or_else(|| build_tool_handlers(&vector));

    let events: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    let events_clone = events.clone();
    let on_event: EventCallback = Box::new(move |event: AgentEvent| {
        let event_type = match &event {
            AgentEvent::Token(_) => "token",
            AgentEvent::Thinking(_) => "thinking",
            AgentEvent::ToolCallStart { .. } => "tool_call_start",
            AgentEvent::ToolResult { .. } => "tool_result",
            AgentEvent::Status(_) => "status",
            AgentEvent::MessagesUpdated { .. } => "messages_updated",
            AgentEvent::Done { .. } => "done",
            AgentEvent::Error(_) => "error",
            AgentEvent::Cancelled => "cancelled",
        };
        events_clone.lock().unwrap().push(event_type.to_string());
    });

    let opts = TurnOptions {
        tools,
        on_event: Some(on_event),
        cancelled,
        ..Default::default()
    };

    prompty::pipeline::register_defaults();

    let result = turn(&agent, None, Some(opts)).await;
    let collected = events.lock().unwrap().clone();
    (result, collected)
}

#[tokio::test]
async fn test_events_basic_tool_loop() {
    let (result, events) = run_vector_with_events("events_basic_tool_loop", None, None).await;
    assert!(result.is_ok(), "expected Ok, got: {result:?}");
    let vector = find_vector("events_basic_tool_loop");
    let expected_result = vector["expected"]["result"].as_str().unwrap();
    assert_eq!(result.unwrap().as_str().unwrap(), expected_result);

    // Verify event sequence contains the expected types
    assert!(
        events.contains(&"tool_call_start".to_string()),
        "missing tool_call_start event in {events:?}"
    );
    assert!(
        events.contains(&"tool_result".to_string()),
        "missing tool_result event in {events:?}"
    );
    assert!(
        events.last() == Some(&"done".to_string()),
        "last event should be 'done', got {events:?}"
    );
}

#[tokio::test]
async fn test_events_no_tools() {
    let (result, events) = run_vector_with_events("events_no_tools", None, None).await;
    assert!(result.is_ok(), "expected Ok, got: {result:?}");
    assert_eq!(result.unwrap().as_str().unwrap(), "2 + 2 equals 4.");

    // No tool events should fire — only done
    assert!(
        events.contains(&"done".to_string()),
        "missing done event in {events:?}"
    );
    assert!(
        !events.contains(&"tool_call_start".to_string()),
        "should NOT have tool_call_start event, got {events:?}"
    );
}

#[tokio::test]
async fn test_events_error_logged() {
    // Tool function raises an error — the vector expects the error string
    // to be returned to the LLM and the loop to continue.
    // In the Rust runtime, tool errors propagate as InvokerError from dispatch_tool.
    // We simulate by providing a tool that returns an error string.
    let mut tools: HashMap<String, ToolHandler> = HashMap::new();
    tools.insert(
        "get_weather".to_string(),
        ToolHandler::Sync(Box::new(|_| {
            Err("RuntimeError: Weather service unavailable".into())
        })),
    );

    let (result, events) = run_vector_with_events("events_error_logged", Some(tools), None).await;

    // The Rust runtime propagates tool errors — this differs from the spec
    // which expects error strings to be fed back to the LLM. We accept
    // either behavior: an error result, or the continued loop result.
    if result.is_err() {
        let err_str = result.unwrap_err().to_string();
        assert!(
            err_str.contains("Weather service unavailable") || err_str.contains("get_weather"),
            "error should mention weather service: {err_str}"
        );
    } else {
        // If the runtime feeds errors back to the LLM (spec behavior)
        assert!(result.unwrap().as_str().is_some());
    }

    assert!(
        events.contains(&"tool_call_start".to_string()),
        "should have tool_call_start event"
    );
}

// ===================================================================
// CANCELLATION VECTORS
// ===================================================================

#[tokio::test]
async fn test_cancellation_before_llm() {
    // Cancel token is already set before the loop starts
    let cancel = Arc::new(AtomicBool::new(true));
    let (result, events) =
        run_vector_with_events("cancellation_before_llm", None, Some(cancel)).await;

    assert!(result.is_err(), "expected cancellation error");
    let err_str = result.unwrap_err().to_string();
    assert!(
        err_str.to_lowercase().contains("cancel"),
        "error should mention cancellation: {err_str}"
    );
    assert!(
        events.contains(&"cancelled".to_string()),
        "should emit cancelled event, got {events:?}"
    );
}

#[tokio::test]
async fn test_cancellation_between_iterations() {
    // Cancel after the first iteration's tool calls are processed
    let vector = find_vector("cancellation_between_iterations");
    let key = mock_key("cancellation_between_iterations");
    let responses = collect_responses(&vector);
    register_mocks(&key, responses);

    let mut agent = build_agent(&vector, &key);
    let input_msgs = &vector["input"]["messages"];
    let mut instruction_lines = Vec::new();
    if let Some(msgs) = input_msgs.as_array() {
        for m in msgs {
            let role = m["role"].as_str().unwrap_or("user");
            let content = m["content"].as_str().unwrap_or("");
            instruction_lines.push(format!("{role}:\n{content}"));
        }
    }
    agent.instructions = Some(instruction_lines.join("\n\n"));

    let cancel = Arc::new(AtomicBool::new(false));
    let cancel_clone = cancel.clone();

    // Build tool handlers that set the cancel flag after the first tool call
    let call_count = Arc::new(AtomicUsize::new(0));
    let call_count_clone = call_count.clone();
    let mut tools: HashMap<String, ToolHandler> = HashMap::new();
    tools.insert(
        "get_weather".to_string(),
        ToolHandler::Sync(Box::new(move |_args| {
            let n = call_count_clone.fetch_add(1, Ordering::SeqCst);
            // After the first tool call completes, set cancel
            if n == 0 {
                cancel_clone.store(true, Ordering::SeqCst);
            }
            Ok("72°F sunny".to_string())
        })),
    );

    let events: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    let events_clone = events.clone();
    let on_event: EventCallback = Box::new(move |event: AgentEvent| {
        let event_type = match &event {
            AgentEvent::Token(_) => "token",
            AgentEvent::Thinking(_) => "thinking",
            AgentEvent::ToolCallStart { .. } => "tool_call_start",
            AgentEvent::ToolResult { .. } => "tool_result",
            AgentEvent::Status(_) => "status",
            AgentEvent::MessagesUpdated { .. } => "messages_updated",
            AgentEvent::Done { .. } => "done",
            AgentEvent::Error(_) => "error",
            AgentEvent::Cancelled => "cancelled",
        };
        events_clone.lock().unwrap().push(event_type.to_string());
    });

    let opts = TurnOptions {
        tools,
        on_event: Some(on_event),
        cancelled: Some(cancel),
        ..Default::default()
    };

    prompty::pipeline::register_defaults();

    let result = turn(&agent, None, Some(opts)).await;
    let collected = events.lock().unwrap().clone();

    assert!(result.is_err(), "expected cancellation error");
    let err_str = result.unwrap_err().to_string();
    assert!(
        err_str.to_lowercase().contains("cancel"),
        "error should mention cancellation: {err_str}"
    );
    assert!(
        collected.contains(&"cancelled".to_string()),
        "should emit cancelled event, got {collected:?}"
    );
}

#[tokio::test]
async fn test_cancellation_between_tools() {
    // LLM requests 2 tool calls. Cancel fires after the first.
    let vector = find_vector("cancellation_between_tools");
    let key = mock_key("cancellation_between_tools");
    let responses = collect_responses(&vector);
    register_mocks(&key, responses);

    let mut agent = build_agent(&vector, &key);
    let input_msgs = &vector["input"]["messages"];
    let mut instruction_lines = Vec::new();
    if let Some(msgs) = input_msgs.as_array() {
        for m in msgs {
            let role = m["role"].as_str().unwrap_or("user");
            let content = m["content"].as_str().unwrap_or("");
            instruction_lines.push(format!("{role}:\n{content}"));
        }
    }
    agent.instructions = Some(instruction_lines.join("\n\n"));

    let cancel = Arc::new(AtomicBool::new(false));
    let cancel_clone = cancel.clone();

    let call_count = Arc::new(AtomicUsize::new(0));
    let call_count_clone = call_count.clone();
    let mut tools: HashMap<String, ToolHandler> = HashMap::new();
    tools.insert(
        "get_weather".to_string(),
        ToolHandler::Sync(Box::new(move |_args| {
            let n = call_count_clone.fetch_add(1, Ordering::SeqCst);
            // After the first tool call, signal cancel
            if n == 0 {
                cancel_clone.store(true, Ordering::SeqCst);
            }
            Ok("72°F sunny".to_string())
        })),
    );

    let opts = TurnOptions {
        tools,
        cancelled: Some(cancel),
        ..Default::default()
    };

    prompty::pipeline::register_defaults();

    let result = turn(&agent, None, Some(opts)).await;

    // Should be cancelled
    assert!(result.is_err(), "expected cancellation error");
    let err_str = result.unwrap_err().to_string();
    assert!(
        err_str.to_lowercase().contains("cancel"),
        "error should mention cancellation: {err_str}"
    );

    // Only one tool call should have been executed
    assert_eq!(
        call_count.load(Ordering::SeqCst),
        1,
        "only 1 tool call should have executed before cancellation"
    );
}

// ===================================================================
// BINDINGS — skip (not yet implemented in Rust runtime)
// ===================================================================

#[tokio::test]
async fn test_bindings_injected() {
    let vector = find_vector("bindings_injected");
    let key = mock_key("bindings_injected");
    let responses = collect_responses(&vector);
    register_mocks(&key, responses);

    let mut agent = build_agent(&vector, &key);
    let input_msgs = &vector["input"]["messages"];
    let mut instruction_lines = Vec::new();
    if let Some(msgs) = input_msgs.as_array() {
        for m in msgs {
            let role = m["role"].as_str().unwrap_or("user");
            let content = m["content"].as_str().unwrap_or("");
            instruction_lines.push(format!("{role}:\n{content}"));
        }
    }
    agent.instructions = Some(instruction_lines.join("\n\n"));

    let captured_args: Arc<Mutex<Option<Value>>> = Arc::new(Mutex::new(None));
    let captured_args_for_tool = Arc::clone(&captured_args);
    let mut tools = HashMap::new();
    tools.insert(
        "get_weather".to_string(),
        ToolHandler::Sync(Box::new(move |args: Value| {
            *captured_args_for_tool.lock().unwrap() = Some(args);
            Ok("22°C sunny".to_string())
        })),
    );
    let opts = TurnOptions {
        tools,
        ..Default::default()
    };

    prompty::pipeline::register_defaults();

    let parent_inputs = vector["input"]["parent_inputs"].clone();
    let result = turn(&agent, Some(&parent_inputs), Some(opts))
        .await
        .unwrap();
    let expected = vector["expected"]["result"].as_str().unwrap();
    assert_eq!(result.as_str().unwrap(), expected);

    let expected_args = &vector["sequence"][0]["expected_execution_args"]["get_weather"];
    let actual_args = captured_args.lock().unwrap().clone().unwrap();
    assert_eq!(&actual_args, expected_args);
}

// ===================================================================
// EXTENSION VECTORS — context trimming, guardrails, steering, parallel
// ===================================================================

/// Helper to build a full TurnOptions from a vector's `input` section,
/// wiring up guardrails, steering, context_budget, parallel_tool_calls,
/// and event collection.
fn build_extension_opts(
    vector: &Value,
    tools: HashMap<String, ToolHandler>,
    events: Option<Arc<Mutex<Vec<String>>>>,
) -> TurnOptions {
    let input = &vector["input"];

    // Context budget
    let context_budget = input
        .get("context_budget")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);

    // Parallel tool calls
    let parallel_tool_calls = input
        .get("parallel_tool_calls")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    // Guardrails
    let guardrails = input.get("guardrails").map(|g| {
        let input_guardrail: Option<prompty::guardrails::InputGuardrail> =
            g.get("input").map(|ig| {
                let action = ig
                    .get("action")
                    .and_then(|a| a.as_str())
                    .unwrap_or("allow")
                    .to_string();
                let reason = ig
                    .get("reason")
                    .and_then(|r| r.as_str())
                    .unwrap_or("")
                    .to_string();
                let f: prompty::guardrails::InputGuardrail = Box::new(move |_msgs, _agent| {
                    let action = action.clone();
                    let reason = reason.clone();
                    Box::pin(async move {
                        if action == "deny" {
                            prompty::guardrails::GuardrailResult::deny(reason)
                        } else {
                            prompty::guardrails::GuardrailResult::allow()
                        }
                    })
                });
                f
            });

        let output_guardrail: Option<prompty::guardrails::OutputGuardrail> =
            g.get("output").map(|og| {
                let action = og
                    .get("action")
                    .and_then(|a| a.as_str())
                    .unwrap_or("allow")
                    .to_string();
                let reason = og
                    .get("reason")
                    .and_then(|r| r.as_str())
                    .unwrap_or("")
                    .to_string();
                let f: prompty::guardrails::OutputGuardrail = Box::new(move |_resp, _agent| {
                    let action = action.clone();
                    let reason = reason.clone();
                    Box::pin(async move {
                        if action == "deny" {
                            prompty::guardrails::GuardrailResult::deny(reason)
                        } else {
                            prompty::guardrails::GuardrailResult::allow()
                        }
                    })
                });
                f
            });

        let tool_guardrail: Option<prompty::guardrails::ToolGuardrail> = g.get("tool").map(|tg| {
            let deny_tools: Vec<String> = tg
                .get("deny_tools")
                .and_then(|d| d.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_str().map(String::from))
                        .collect()
                })
                .unwrap_or_default();
            let reason = tg
                .get("reason")
                .and_then(|r| r.as_str())
                .unwrap_or("Tool denied")
                .to_string();
            let f: prompty::guardrails::ToolGuardrail =
                Box::new(move |tool_name, _args, _agent| {
                    let denied = deny_tools.contains(&tool_name.to_string());
                    let reason = reason.clone();
                    Box::pin(async move {
                        if denied {
                            prompty::guardrails::GuardrailResult::deny(reason)
                        } else {
                            prompty::guardrails::GuardrailResult::allow()
                        }
                    })
                });
            f
        });

        prompty::guardrails::Guardrails {
            input: input_guardrail,
            output: output_guardrail,
            tool: tool_guardrail,
        }
    });

    // Steering
    let steering = input.get("steering").map(|s| {
        let mut steering = prompty::steering::Steering::new();
        if let Some(msgs) = s.get("messages").and_then(|m| m.as_array()) {
            for msg in msgs {
                let text = msg.get("text").and_then(|t| t.as_str()).unwrap_or("");
                // All steering messages in our vectors have inject_before_iteration: 2
                // The Steering queue is drained at the start of each iteration,
                // so we pre-load them — they'll be drained before iteration 2 (index 1).
                steering.send(text);
            }
        }
        steering
    });

    // Event callback
    let on_event: Option<EventCallback> = events.map(|ev| {
        let f: EventCallback = Box::new(move |event: AgentEvent| {
            let label = match &event {
                AgentEvent::ToolCallStart { name, .. } => format!("ToolCallStart:{name}"),
                AgentEvent::ToolResult { name, .. } => format!("ToolResult:{name}"),
                AgentEvent::MessagesUpdated { .. } => "MessagesUpdated".into(),
                AgentEvent::Done { .. } => "Done".into(),
                AgentEvent::Error(e) => format!("Error:{e}"),
                AgentEvent::Cancelled => "Cancelled".into(),
                _ => format!("{event:?}"),
            };
            ev.lock().unwrap().push(label);
        });
        f
    });

    TurnOptions {
        tools,
        context_budget,
        parallel_tool_calls,
        guardrails,
        steering,
        on_event,
        ..Default::default()
    }
}

/// Helper for running extension vectors — like run_vector but with extension TurnOptions.
async fn run_extension_vector(vector_name: &str) -> Result<Value, InvokerError> {
    run_extension_vector_with_events(vector_name, None).await
}

async fn run_extension_vector_with_events(
    vector_name: &str,
    events: Option<Arc<Mutex<Vec<String>>>>,
) -> Result<Value, InvokerError> {
    let vector = find_vector(vector_name);
    let key = mock_key(vector_name);
    let responses = collect_responses(&vector);
    register_mocks(&key, responses);

    let mut agent = build_agent(&vector, &key);
    let input_msgs = &vector["input"]["messages"];
    let mut instruction_lines = Vec::new();
    if let Some(msgs) = input_msgs.as_array() {
        for m in msgs {
            let role = m["role"].as_str().unwrap_or("user");
            let content = m["content"].as_str().unwrap_or("");
            instruction_lines.push(format!("{role}:\n{content}"));
        }
    }
    agent.instructions = Some(instruction_lines.join("\n\n"));

    let tools = build_tool_handlers(&vector);
    let opts = build_extension_opts(&vector, tools, events);

    prompty::pipeline::register_defaults();

    turn(&agent, None, Some(opts)).await
}

// -------------------------------------------------------------------
// Context trimming (§13.3)
// -------------------------------------------------------------------

#[tokio::test]
async fn test_context_trim_basic() {
    // Vector has many long messages that exceed context_budget.
    // After trimming, oldest non-system messages are dropped and a summary inserted.
    let vector = find_vector("context_trim_basic");
    let expected_result = vector["expected"]["result"].as_str().unwrap();

    let result = run_extension_vector("context_trim_basic").await.unwrap();
    assert_eq!(result.as_str().unwrap(), expected_result);
}

#[tokio::test]
async fn test_context_no_trim_when_fits() {
    // Messages fit within budget — no trimming occurs.
    let vector = find_vector("context_no_trim_when_fits");
    let expected_result = vector["expected"]["result"].as_str().unwrap();

    let result = run_extension_vector("context_no_trim_when_fits")
        .await
        .unwrap();
    assert_eq!(result.as_str().unwrap(), expected_result);
}

#[tokio::test]
async fn test_context_preserves_system_messages() {
    // Two system messages + many exchanges. Both system messages MUST survive trimming.
    let vector = find_vector("context_preserves_system_messages");
    let expected_result = vector["expected"]["result"].as_str().unwrap();

    let result = run_extension_vector("context_preserves_system_messages")
        .await
        .unwrap();
    assert_eq!(result.as_str().unwrap(), expected_result);
}

// -------------------------------------------------------------------
// Guardrails (§13.4)
// -------------------------------------------------------------------

#[tokio::test]
async fn test_guardrail_input_deny() {
    // Input guardrail denies before any LLM call.
    let result = run_extension_vector("guardrail_input_deny").await;
    assert!(result.is_err(), "Expected error from input guardrail");
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("guardrail")
            || err_msg.contains("Guardrail")
            || err_msg.contains("denied"),
        "Error should mention guardrail: {err_msg}"
    );
    assert!(
        err_msg.contains("PII") || err_msg.contains("Contains PII"),
        "Error should mention denial reason: {err_msg}"
    );
}

#[tokio::test]
async fn test_guardrail_output_deny() {
    // Input guardrail passes, LLM responds, output guardrail denies.
    let result = run_extension_vector("guardrail_output_deny").await;
    assert!(result.is_err(), "Expected error from output guardrail");
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("guardrail")
            || err_msg.contains("Guardrail")
            || err_msg.contains("denied"),
        "Error should mention guardrail: {err_msg}"
    );
    assert!(
        err_msg.contains("harmful") || err_msg.contains("Response contains harmful"),
        "Error should mention denial reason: {err_msg}"
    );
}

#[tokio::test]
async fn test_guardrail_tool_deny() {
    // Two tool calls — dangerous_tool is denied, get_weather executes normally.
    let vector = find_vector("guardrail_tool_deny");
    let expected_result = vector["expected"]["result"].as_str().unwrap();

    let result = run_extension_vector("guardrail_tool_deny").await.unwrap();
    assert_eq!(result.as_str().unwrap(), expected_result);
}

#[tokio::test]
async fn test_guardrail_all_pass() {
    // All guardrails configured but all pass — normal agent loop completes.
    let vector = find_vector("guardrail_all_pass");
    let expected_result = vector["expected"]["result"].as_str().unwrap();

    let result = run_extension_vector("guardrail_all_pass").await.unwrap();
    assert_eq!(result.as_str().unwrap(), expected_result);
}

// -------------------------------------------------------------------
// Steering (§13.5)
// -------------------------------------------------------------------

#[tokio::test]
async fn test_steering_inject_message() {
    // Steering message injected before iteration 2.
    let vector = find_vector("steering_inject_message");
    let expected_result = vector["expected"]["result"].as_str().unwrap();

    let result = run_extension_vector("steering_inject_message")
        .await
        .unwrap();
    assert_eq!(result.as_str().unwrap(), expected_result);
}

#[tokio::test]
async fn test_steering_multiple_messages() {
    // Two steering messages injected before iteration 2.
    let vector = find_vector("steering_multiple_messages");
    let expected_result = vector["expected"]["result"].as_str().unwrap();

    let result = run_extension_vector("steering_multiple_messages")
        .await
        .unwrap();
    assert_eq!(result.as_str().unwrap(), expected_result);
}

// -------------------------------------------------------------------
// Parallel tool calls (§13.6)
// -------------------------------------------------------------------

#[tokio::test]
async fn test_parallel_tools_basic() {
    // 3 tool calls in one turn, dispatched in parallel.
    let vector = find_vector("parallel_tools_basic");
    let expected_result = vector["expected"]["result"].as_str().unwrap();

    let result = run_extension_vector("parallel_tools_basic").await.unwrap();
    assert_eq!(result.as_str().unwrap(), expected_result);
}

#[tokio::test]
async fn test_parallel_tools_with_guardrail_deny() {
    // 3 parallel tool calls — one denied by tool guardrail, two allowed.
    let vector = find_vector("parallel_tools_with_guardrail_deny");
    let expected_result = vector["expected"]["result"].as_str().unwrap();

    let result = run_extension_vector("parallel_tools_with_guardrail_deny")
        .await
        .unwrap();
    assert_eq!(result.as_str().unwrap(), expected_result);
}