rig-agent 0.43.0

Rig's classic agent runtime.
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
use std::sync::{
    Arc, Mutex,
    atomic::{AtomicUsize, Ordering},
};

use serde_json::json;

use super::*;
use crate::agent::{HookContext, ModelTurnAction, OutcomeAction, OutcomeEvent};
use crate::completion::{PromptError, StructuredOutputError, Usage};
use crate::test_utils::{MockCompletionModel, MockTurn};
use rig_core::message::{AssistantContent, ToolCall, ToolFunction};
use rig_core::vector_store::{
    VectorSearchRequest, VectorStoreError, VectorStoreIndex, request::Filter,
};
use serde::Deserialize;

#[derive(Debug, PartialEq, Deserialize, Serialize, JsonSchema)]
struct Person {
    name: String,
}

fn usage(total_tokens: u64) -> Usage {
    Usage {
        total_tokens: Some(total_tokens),
        ..Usage::default()
    }
}

fn extractor(model: MockCompletionModel, retries: u64) -> Extractor<Person> {
    ExtractorBuilder::new(model).retries(retries).build()
}

fn submit_turn(name: &str) -> MockTurn {
    MockTurn::tool_call("id1", SUBMIT_TOOL_NAME, json!({ "name": name }))
}

fn tool_call(id: &str, name: &str, arguments: serde_json::Value) -> AssistantContent {
    AssistantContent::ToolCall(ToolCall::from_wire(
        id,
        ToolFunction::new(
            rig_core::message::ToolName::new(name.to_string()).expect("tool name"),
            arguments,
        ),
    ))
}

#[derive(Clone, Default)]
struct LifecycleCounts {
    completion_calls: Arc<AtomicUsize>,
    completion_responses: Arc<AtomicUsize>,
    model_turns: Arc<AtomicUsize>,
    invalid_tool_calls: Arc<AtomicUsize>,
}

impl AgentHook for LifecycleCounts {
    async fn on_completion_call(
        &self,
        _ctx: &HookContext,
        _event: crate::agent::CompletionCallEvent<'_>,
    ) -> crate::agent::CompletionCallAction {
        self.completion_calls.fetch_add(1, Ordering::SeqCst);
        crate::agent::CompletionCallAction::Continue
    }

    async fn on_outcome(&self, _ctx: &HookContext, event: OutcomeEvent<'_>) -> OutcomeAction {
        if event.completion().is_some() {
            self.completion_responses.fetch_add(1, Ordering::SeqCst);
        }
        OutcomeAction::Proceed
    }

    async fn on_model_turn_finished(
        &self,
        _ctx: &HookContext,
        _event: crate::agent::ModelTurnFinished<'_>,
    ) -> ModelTurnAction {
        self.model_turns.fetch_add(1, Ordering::SeqCst);
        ModelTurnAction::Continue
    }

    async fn on_invalid_tool_call(
        &self,
        _ctx: &HookContext,
        _event: &crate::agent::InvalidToolCallContext,
    ) -> Option<crate::agent::InvalidToolCallAction> {
        self.invalid_tool_calls.fetch_add(1, Ordering::SeqCst);
        None
    }
}

type ExtractorResponseSnapshot = (Message, Vec<AssistantContent>, Usage, Option<String>);

#[derive(Clone, Default)]
struct ExtractorResponseCapture {
    prompt: Arc<Mutex<Option<Message>>>,
    snapshot: Arc<Mutex<Option<ExtractorResponseSnapshot>>>,
}

impl AgentHook for ExtractorResponseCapture {
    async fn on_completion_call(
        &self,
        _ctx: &HookContext,
        event: crate::agent::CompletionCallEvent<'_>,
    ) -> crate::agent::CompletionCallAction {
        *self.prompt.lock().expect("extractor prompt") = Some(event.prompt.clone());
        crate::agent::CompletionCallAction::continue_run()
    }

    async fn on_outcome(&self, _ctx: &HookContext, event: OutcomeEvent<'_>) -> OutcomeAction {
        let Some(response) = event.completion() else {
            return OutcomeAction::proceed();
        };
        let prompt = self
            .prompt
            .lock()
            .expect("extractor prompt")
            .clone()
            .expect("the completion call precedes its outcome");
        *self.snapshot.lock().expect("extractor response snapshot") = Some((
            prompt,
            response.choice.clone(),
            response.usage,
            response.message_id.clone(),
        ));
        OutcomeAction::proceed()
    }
}

struct StopBeforeCompletion;

impl AgentHook for StopBeforeCompletion {
    async fn on_completion_call(
        &self,
        _ctx: &HookContext,
        _event: crate::agent::CompletionCallEvent<'_>,
    ) -> crate::agent::CompletionCallAction {
        crate::agent::CompletionCallAction::stop("extractor stopped")
    }
}

struct ExtractorContextIndex {
    queries: Arc<Mutex<Vec<(String, u64)>>>,
}

impl VectorStoreIndex for ExtractorContextIndex {
    type Filter = Filter<serde_json::Value>;

    async fn top_n<T: DeserializeOwned + WasmCompatSend>(
        &self,
        req: VectorSearchRequest,
    ) -> Result<Vec<(f64, String, T)>, VectorStoreError> {
        self.queries
            .lock()
            .expect("extractor query recorder")
            .push((req.query().to_string(), req.samples()));
        let value = serde_json::from_value(json!({ "question": "retrieved" }))?;
        Ok(vec![(1.0, "extractor-context".to_string(), value)])
    }

    async fn top_n_ids(
        &self,
        _req: VectorSearchRequest,
    ) -> Result<Vec<(f64, String)>, VectorStoreError> {
        Ok(vec![(1.0, "extractor-context".to_string())])
    }
}

#[derive(Clone, Copy)]
enum StopFirstBilledResponseAt {
    CompletionOutcome,
    ModelTurnFinished,
}

#[derive(Clone)]
struct StopFirstBilledResponse {
    phase: StopFirstBilledResponseAt,
    calls: Arc<AtomicUsize>,
}

impl AgentHook for StopFirstBilledResponse {
    async fn on_outcome(&self, _ctx: &HookContext, event: OutcomeEvent<'_>) -> OutcomeAction {
        if event.completion().is_some()
            && matches!(self.phase, StopFirstBilledResponseAt::CompletionOutcome)
            && self.calls.fetch_add(1, Ordering::SeqCst) == 0
        {
            OutcomeAction::stop("stop first billed response")
        } else {
            OutcomeAction::proceed()
        }
    }

    async fn on_model_turn_finished(
        &self,
        _ctx: &HookContext,
        _event: crate::agent::ModelTurnFinished<'_>,
    ) -> ModelTurnAction {
        if matches!(self.phase, StopFirstBilledResponseAt::ModelTurnFinished)
            && self.calls.fetch_add(1, Ordering::SeqCst) == 0
        {
            ModelTurnAction::stop("stop first billed model turn")
        } else {
            ModelTurnAction::continue_run()
        }
    }
}

struct StopOnInvalidToolCall;

impl AgentHook for StopOnInvalidToolCall {
    async fn on_invalid_tool_call(
        &self,
        _ctx: &HookContext,
        _event: &crate::agent::InvalidToolCallContext,
    ) -> Option<crate::agent::InvalidToolCallAction> {
        Some(crate::agent::InvalidToolCallAction::stop(
            "unexpected extractor tool call",
        ))
    }
}

struct RepairUnexpectedAsSubmit;

impl AgentHook for RepairUnexpectedAsSubmit {
    async fn on_invalid_tool_call(
        &self,
        _ctx: &HookContext,
        _event: &crate::agent::InvalidToolCallContext,
    ) -> Option<crate::agent::InvalidToolCallAction> {
        Some(crate::agent::InvalidToolCallAction::repair(
            SUBMIT_TOOL_NAME,
        ))
    }
}

struct SkipUnexpected;

impl AgentHook for SkipUnexpected {
    async fn on_invalid_tool_call(
        &self,
        _ctx: &HookContext,
        _event: &crate::agent::InvalidToolCallContext,
    ) -> Option<crate::agent::InvalidToolCallAction> {
        Some(crate::agent::InvalidToolCallAction::skip(
            "ignored by extractor hook",
        ))
    }
}

#[tokio::test]
async fn extractor_runs_through_full_response_lifecycle() {
    let model = MockCompletionModel::from_turns([submit_turn("John")]);
    let counts = LifecycleCounts::default();
    let response = ExtractorBuilder::<Person>::new(model.clone())
        .add_hook(counts.clone())
        .build()
        .extract("John")
        .await
        .expect("extraction should succeed");

    assert_eq!(response.output.name, "John");
    assert_eq!(model.request_count(), 1);
    assert_eq!(counts.completion_calls.load(Ordering::SeqCst), 1);
    assert_eq!(counts.completion_responses.load(Ordering::SeqCst), 1);
    assert_eq!(counts.model_turns.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn extractor_hook_receives_canonical_response_fields() {
    let capture = ExtractorResponseCapture::default();
    let expected_usage = usage(23);
    let response =
        ExtractorBuilder::<Person>::new(MockCompletionModel::from_turns([submit_turn("John")
            .with_usage(expected_usage)
            .with_message_id("extractor-message")]))
        .add_hook(capture.clone())
        .build()
        .extract("John")
        .await
        .expect("extraction should succeed");
    assert_eq!(response.output.name, "John");

    let (prompt, content, observed_usage, message_id) = capture
        .snapshot
        .lock()
        .expect("extractor response snapshot")
        .clone()
        .expect("extractor response hook should fire");
    assert_eq!(prompt, Message::user("John"));
    assert_eq!(observed_usage, expected_usage);
    assert_eq!(message_id.as_deref(), Some("extractor-message"));
    assert!(matches!(
        content.as_slice(),
        [AssistantContent::ToolCall(tool_call)]
            if tool_call.function.name == SUBMIT_TOOL_NAME
                && tool_call.function.arguments == json!({"name": "John"})
    ));
}

#[tokio::test]
async fn extractor_dynamic_context_uses_the_agent_hook_lifecycle() {
    let model = MockCompletionModel::from_turns([submit_turn("John")]);
    let probe = model.clone();
    let queries = Arc::new(Mutex::new(Vec::new()));
    let response = ExtractorBuilder::<Person>::new(model)
        .dynamic_context(
            2,
            ExtractorContextIndex {
                queries: queries.clone(),
            },
        )
        .build()
        .extract("John")
        .await
        .expect("extraction should succeed");

    assert_eq!(response.output.name, "John");
    assert_eq!(
        *queries.lock().expect("extractor queries"),
        vec![("John".to_string(), 2)]
    );
    let requests = probe.requests();
    let request = requests.first().expect("one extractor request");
    assert!(
        request
            .documents
            .iter()
            .any(|document| document.id == "extractor-context"
                && document.text == "{\n  \"question\": \"retrieved\"\n}")
    );
}

#[tokio::test]
async fn extractor_completion_call_stop_prevents_provider_io() {
    let model = MockCompletionModel::from_turns([submit_turn("John")]);
    let error = ExtractorBuilder::<Person>::new(model.clone())
        .add_hook(StopBeforeCompletion)
        .build()
        .extract("John")
        .await
        .expect_err("terminating hook should cancel extraction");

    assert!(matches!(
        error,
        StructuredOutputError::PromptError(err)
            if matches!(
                err,
                PromptError::PromptCancelled { ref reason, .. } if reason == "extractor stopped"
            )
    ));
    assert_eq!(model.request_count(), 0);
}

#[tokio::test]
async fn usage_accumulates_across_failed_attempts() {
    let model = MockCompletionModel::from_turns([
        MockTurn::text("no submit call").with_usage(usage(10)),
        submit_turn("John").with_usage(usage(5)),
    ]);

    let response = extractor(model, 1)
        .extract("John")
        .await
        .expect("second attempt should succeed");

    assert_eq!(
        response.output,
        Person {
            name: "John".to_string()
        }
    );
    assert_eq!(response.usage.total_tokens, Some(15));
}

async fn assert_billed_hook_termination_usage(phase: StopFirstBilledResponseAt) {
    let model = MockCompletionModel::from_turns([
        submit_turn("ignored").with_usage(usage(10)),
        submit_turn("John").with_usage(usage(5)),
    ]);
    let response = ExtractorBuilder::<Person>::new(model)
        .retries(1)
        .add_hook(StopFirstBilledResponse {
            phase,
            calls: Arc::new(AtomicUsize::new(0)),
        })
        .build()
        .extract("John")
        .await
        .expect("second attempt should succeed");

    assert_eq!(response.output.name, "John");
    assert_eq!(response.usage.total_tokens, Some(15));
}

#[tokio::test]
async fn completion_outcome_hook_termination_preserves_billed_usage() {
    assert_billed_hook_termination_usage(StopFirstBilledResponseAt::CompletionOutcome).await;
}

#[tokio::test]
async fn model_turn_finished_hook_termination_preserves_billed_usage() {
    assert_billed_hook_termination_usage(StopFirstBilledResponseAt::ModelTurnFinished).await;
}

#[tokio::test]
async fn unexpected_tool_call_preserves_usage_and_retries() {
    let model = MockCompletionModel::from_turns([
        MockTurn::tool_call("unknown", "unexpected", json!({})).with_usage(usage(10)),
        submit_turn("John").with_usage(usage(5)),
    ]);

    let response = extractor(model, 1)
        .extract("John")
        .await
        .expect("second attempt should succeed");

    assert_eq!(response.output.name, "John");
    assert_eq!(response.usage.total_tokens, Some(15));
}

#[tokio::test]
async fn unexpected_tool_call_runs_hooks_before_extractor_fallback() {
    let model = MockCompletionModel::from_turns([
        MockTurn::tool_call("unknown", "unexpected", json!({})).with_usage(usage(10)),
        submit_turn("John").with_usage(usage(5)),
    ]);
    let counts = LifecycleCounts::default();

    let response = ExtractorBuilder::<Person>::new(model)
        .retries(1)
        .add_hook(counts.clone())
        .build()
        .extract("John")
        .await
        .expect("deferred invalid call should use extractor fallback");

    assert_eq!(response.output.name, "John");
    assert_eq!(response.usage.total_tokens, Some(15));
    assert_eq!(counts.invalid_tool_calls.load(Ordering::SeqCst), 1);
    assert_eq!(counts.completion_responses.load(Ordering::SeqCst), 2);
    assert_eq!(counts.model_turns.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn unexpected_tool_call_hook_can_stop_extraction() {
    let model =
        MockCompletionModel::from_turns([MockTurn::tool_call("unknown", "unexpected", json!({}))]);

    let error = ExtractorBuilder::<Person>::new(model)
        .add_hook(StopOnInvalidToolCall)
        .build()
        .extract("John")
        .await
        .expect_err("invalid-tool hook should retain control");

    assert!(matches!(
        error,
        StructuredOutputError::PromptError(err)
            if matches!(
                err,
                PromptError::PromptCancelled { ref reason, .. }
                    if reason == "unexpected extractor tool call"
            )
    ));
}

#[tokio::test]
async fn unexpected_tool_call_hook_can_repair_to_submit() {
    let model = MockCompletionModel::from_turns([MockTurn::tool_call(
        "unknown",
        "unexpected",
        json!({ "name": "John" }),
    )]);

    let response = ExtractorBuilder::<Person>::new(model)
        .add_hook(RepairUnexpectedAsSubmit)
        .build()
        .extract("John")
        .await
        .expect("repaired output-tool call should finalize extraction");

    assert_eq!(response.output.name, "John");
}

#[tokio::test]
async fn skip_hook_preserves_valid_submit_sibling() {
    let turn = MockTurn::from_contents([
        tool_call("unknown", "unexpected", json!({})),
        tool_call("submit", SUBMIT_TOOL_NAME, json!({ "name": "John" })),
    ]);
    let model = MockCompletionModel::from_turns([turn]);

    let response = ExtractorBuilder::<Person>::new(model)
        .add_hook(SkipUnexpected)
        .build()
        .extract("John")
        .await
        .expect("skipping an invalid sibling should preserve submit");

    assert_eq!(response.output.name, "John");
}

#[tokio::test]
async fn submit_call_wins_over_unexpected_sibling_call() {
    let turn = MockTurn::from_contents([
        tool_call("unknown", "unexpected", json!({})),
        tool_call("submit", SUBMIT_TOOL_NAME, json!({ "name": "John" })),
    ])
    .with_usage(usage(7));
    let model = MockCompletionModel::from_turns([turn]);

    let response = extractor(model, 0)
        .extract("John")
        .await
        .expect("submit should remain authoritative");

    assert_eq!(response.output.name, "John");
    assert_eq!(response.usage.total_tokens, Some(7));
}

#[tokio::test]
async fn submit_call_wins_before_unexpected_sibling_call() {
    let turn = MockTurn::from_contents([
        tool_call("submit", SUBMIT_TOOL_NAME, json!({ "name": "John" })),
        tool_call("unknown", "unexpected", json!({})),
    ]);

    let response = extractor(MockCompletionModel::from_turns([turn]), 0)
        .extract("John")
        .await
        .expect("an earlier submit should remain authoritative");

    assert_eq!(response.output.name, "John");
}

#[tokio::test]
async fn multiple_unexpected_calls_surrounding_submit_are_ignored() {
    let turn = MockTurn::from_contents([
        tool_call("unknown-before", "unexpected_before", json!({})),
        tool_call("submit", SUBMIT_TOOL_NAME, json!({ "name": "John" })),
        tool_call("unknown-after", "unexpected_after", json!({})),
    ]);

    let response = extractor(MockCompletionModel::from_turns([turn]), 0)
        .extract("John")
        .await
        .expect("unexpected siblings should not displace submit");

    assert_eq!(response.output.name, "John");
}

#[tokio::test]
async fn transport_errors_contribute_no_usage() {
    let model = MockCompletionModel::from_turns([
        MockTurn::error("boom"),
        submit_turn("John").with_usage(usage(5)),
    ]);

    let response = extractor(model, 1)
        .extract("John")
        .await
        .expect("second attempt should succeed");

    assert_eq!(response.usage.total_tokens, Some(5));
}

#[tokio::test]
async fn single_successful_attempt_reports_its_own_usage() {
    let model = MockCompletionModel::from_turns([submit_turn("John").with_usage(usage(7))]);

    let response = extractor(model, 0)
        .extract("John")
        .await
        .expect("extraction should succeed");

    assert_eq!(response.usage.total_tokens, Some(7));
}

#[tokio::test]
async fn exhausted_retries_return_last_error() {
    let model =
        MockCompletionModel::from_turns([MockTurn::text("no submit call").with_usage(usage(10))]);

    let err = extractor(model, 0)
        .extract("John")
        .await
        .expect_err("extraction should fail");

    assert!(matches!(err, StructuredOutputError::EmptyResponse));
}

#[tokio::test]
async fn exhausted_retries_return_error_from_final_attempt() {
    let model =
        MockCompletionModel::from_turns([MockTurn::error("first"), MockTurn::error("second")]);

    let err = extractor(model, 1)
        .extract("John")
        .await
        .expect_err("extraction should fail");

    assert!(matches!(
        err,
        StructuredOutputError::PromptError(err)
            if matches!(
                err,
                PromptError::Report(ref report)
                    if report.kind == rig_core::error::ErrorKind::Provider
                        && report.message.ends_with("second")
            )
    ));
}