starweaver-runtime 0.10.0

Agent-loop graph and runtime executor primitives for Starweaver
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
#![allow(missing_docs, clippy::unwrap_used)]

use std::sync::{
    Arc, Mutex,
    atomic::{AtomicUsize, Ordering},
};

use async_trait::async_trait;
use starweaver_context::{AgentContext, BusMessage};
use starweaver_model::{
    ContentPart, ModelMessage, ModelRequestPart, ModelResponse, ModelResponsePart, TestModel,
    ToolCallPart, tool_call_response,
};
use starweaver_runtime::{
    Agent, AgentCapability, AgentEndStrategy, AgentRunState, AgentRuntimePolicy, AgentStreamEvent,
    CapabilityError, FunctionOutputFunction, OutputFunctionContext, OutputFunctionDefinition,
    OutputValidationError, OutputValidationResult, OutputValidator, OutputValue,
};

struct RequiresParis;

#[async_trait]
impl OutputValidator for RequiresParis {
    async fn validate(
        &self,
        _state: &mut AgentRunState,
        output: &OutputValue,
    ) -> OutputValidationResult<()> {
        let value = output.parse::<serde_json::Value>()?;
        if value["answer"] == "Paris" {
            Ok(())
        } else {
            Err(OutputValidationError::retry("answer must be Paris"))
        }
    }
}

fn final_answer_function() -> FunctionOutputFunction<
    impl Send
    + Sync
    + Fn(
        OutputFunctionContext,
        serde_json::Value,
    ) -> std::future::Ready<Result<OutputValue, OutputValidationError>>,
> {
    FunctionOutputFunction::new(
        OutputFunctionDefinition::new(
            "final_answer",
            serde_json::json!({
                "type": "object",
                "properties": {"answer": {"type": "string"}},
                "required": ["answer"]
            }),
        )
        .with_description("Return the final answer"),
        |_ctx, args: serde_json::Value| {
            std::future::ready(Ok(OutputValue::Json(serde_json::json!({
                "answer": args["answer"].as_str().unwrap_or_default()
            }))))
        },
    )
}

fn output_and_lookup_response() -> ModelResponse {
    let mut response = tool_call_response(
        "call_output",
        "final_answer",
        serde_json::json!({"answer": "Paris"}),
    );
    response
        .parts
        .push(ModelResponsePart::ToolCall(ToolCallPart {
            id: "call_lookup".to_string(),
            name: "lookup".to_string(),
            arguments: serde_json::json!({"query": "Paris"}).into(),
        }));
    response
}

#[tokio::test]
async fn output_function_call_finishes_run() {
    let model = Arc::new(TestModel::with_responses(vec![tool_call_response(
        "call_1",
        "final_answer",
        serde_json::json!({"answer": "Paris"}),
    )]));

    let result = Agent::new(model.clone())
        .with_output_function(Arc::new(final_answer_function()))
        .run("answer")
        .await
        .unwrap();

    assert_eq!(result.output, r#"{"answer":"Paris"}"#);
    assert_eq!(result.structured_output.unwrap()["answer"], "Paris");
    assert_eq!(model.captured_params()[0].tools[0].name, "final_answer");
}

#[tokio::test]
async fn early_end_strategy_skips_ordinary_tools_after_output_function() {
    let model = Arc::new(TestModel::with_responses(
        vec![output_and_lookup_response()],
    ));
    let lookup_calls = Arc::new(AtomicUsize::new(0));
    let lookup_counter = lookup_calls.clone();
    let lookup = starweaver_tools::FunctionTool::new(
        "lookup",
        Some("Lookup".to_string()),
        serde_json::json!({"type": "object"}),
        move |_ctx, args| {
            lookup_counter.fetch_add(1, Ordering::SeqCst);
            async move { Ok(starweaver_tools::ToolResult::new(args)) }
        },
    );

    let result = Agent::new(model.clone())
        .with_output_function(Arc::new(final_answer_function()))
        .with_tools(starweaver_tools::ToolRegistry::new().with_tool(Arc::new(lookup)))
        .run("answer")
        .await
        .unwrap();

    assert_eq!(result.output, r#"{"answer":"Paris"}"#);
    assert_eq!(lookup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(model.captured_messages().len(), 1);
    assert!(!result
        .messages
        .iter()
        .any(|message| matches!(message, ModelMessage::Request(request) if request.parts.iter().any(|part| matches!(part, starweaver_model::ModelRequestPart::ToolReturn(_))))));
}

#[tokio::test]
async fn exhaustive_end_strategy_runs_ordinary_tools_after_output_function() {
    let model = Arc::new(TestModel::with_responses(
        vec![output_and_lookup_response()],
    ));
    let lookup_calls = Arc::new(AtomicUsize::new(0));
    let lookup_counter = lookup_calls.clone();
    let lookup = starweaver_tools::FunctionTool::new(
        "lookup",
        Some("Lookup".to_string()),
        serde_json::json!({"type": "object"}),
        move |_ctx, args| {
            lookup_counter.fetch_add(1, Ordering::SeqCst);
            async move { Ok(starweaver_tools::ToolResult::new(args)) }
        },
    );

    let result = Agent::new(model.clone())
        .with_output_function(Arc::new(final_answer_function()))
        .with_tools(starweaver_tools::ToolRegistry::new().with_tool(Arc::new(lookup)))
        .with_policy(AgentRuntimePolicy {
            end_strategy: AgentEndStrategy::Exhaustive,
            ..AgentRuntimePolicy::default()
        })
        .run("answer")
        .await
        .unwrap();

    assert_eq!(result.output, r#"{"answer":"Paris"}"#);
    assert_eq!(lookup_calls.load(Ordering::SeqCst), 1);
    assert_eq!(model.captured_messages().len(), 1);
    assert!(result.messages.iter().any(|message| {
        matches!(message, ModelMessage::Request(request)
            if request.metadata.get("starweaver.final_output_tool_returns") == Some(&serde_json::json!(true))
                && request.parts.iter().any(|part| matches!(part, starweaver_model::ModelRequestPart::ToolReturn(tool_return) if tool_return.name == "lookup")))
    }));
}

#[tokio::test]
async fn output_function_retry_sends_retry_prompt_and_accepts_next_call() {
    let retry_function = FunctionOutputFunction::new(
        OutputFunctionDefinition::new(
            "final_answer",
            serde_json::json!({"type": "object", "required": ["answer"]}),
        ),
        |_ctx, args: serde_json::Value| {
            let answer = args["answer"].as_str().unwrap_or_default().to_string();
            async move {
                if answer == "Paris" {
                    Ok(OutputValue::Json(serde_json::json!({"answer": answer})))
                } else {
                    Err(OutputValidationError::retry("answer must be Paris"))
                }
            }
        },
    );
    let model = Arc::new(TestModel::with_responses(vec![
        tool_call_response(
            "call_1",
            "final_answer",
            serde_json::json!({"answer": "London"}),
        ),
        tool_call_response(
            "call_2",
            "final_answer",
            serde_json::json!({"answer": "Paris"}),
        ),
    ]));

    let result = Agent::new(model.clone())
        .with_output_function(Arc::new(retry_function))
        .with_policy(AgentRuntimePolicy {
            max_steps: 3,
            output_retries: 1,
            ..AgentRuntimePolicy::default()
        })
        .run("answer")
        .await
        .unwrap();

    assert_eq!(result.output, r#"{"answer":"Paris"}"#);
    assert_eq!(result.structured_output.unwrap()["answer"], "Paris");
    assert_eq!(model.captured_messages().len(), 2);
    assert!(format!("{:?}", model.captured_messages()[1]).contains("answer must be Paris"));
}

#[derive(Default)]
struct InjectSteeringOnFirstOutput {
    injected: Mutex<bool>,
}

#[async_trait]
impl AgentCapability for InjectSteeringOnFirstOutput {
    async fn validate_output_with_context(
        &self,
        _state: &mut AgentRunState,
        context: &mut AgentContext,
        _output: &str,
    ) -> Result<(), CapabilityError> {
        let mut injected = self.injected.lock().unwrap();
        if !*injected {
            context.enqueue_message(BusMessage::new(
                "steering",
                serde_json::json!({"id": "late", "text": "reconsider the final answer"}),
            ));
            *injected = true;
        }
        drop(injected);
        Ok(())
    }
}

#[tokio::test]
async fn output_function_steering_transition_reenters_request_without_output_retry() {
    let model = Arc::new(TestModel::with_responses(vec![
        tool_call_response(
            "call_1",
            "final_answer",
            serde_json::json!({"answer": "Paris"}),
        ),
        tool_call_response(
            "call_2",
            "final_answer",
            serde_json::json!({"answer": "Paris"}),
        ),
    ]));
    let mut context = AgentContext::default();
    let mut events = Vec::new();

    let result = Agent::new(model.clone())
        .with_output_function(Arc::new(final_answer_function()))
        .with_capability(Arc::new(InjectSteeringOnFirstOutput::default()))
        .run_with_context_and_stream_events("answer", &mut context, &mut events)
        .await
        .unwrap();

    assert_eq!(result.output, r#"{"answer":"Paris"}"#);
    let captured_messages = model.captured_messages();
    assert_eq!(captured_messages.len(), 2);
    let Some(second_request) =
        captured_messages[1]
            .iter()
            .rev()
            .find_map(|message| match message {
                ModelMessage::Request(request) => Some(request),
                ModelMessage::Response(_) => None,
            })
    else {
        panic!("missing second model request");
    };
    let expected_guard = ModelRequestPart::Instruction {
        text: "<system-reminder>There are pending steering messages. Continue and incorporate them before finalizing.</system-reminder>".to_string(),
        metadata: serde_json::json!({
            "starweaver.kind": "steering_guard",
            "starweaver_instruction_dynamic": true,
            "starweaver_instruction_origin": "dynamic_instruction",
        })
        .as_object()
        .cloned()
        .unwrap_or_default(),
    };
    let expected_steering = ModelRequestPart::UserPrompt {
        content: vec![ContentPart::Text {
            text: "Steering update from the user:\nreconsider the final answer".to_string(),
        }],
        name: Some("steering".to_string()),
        metadata: serde_json::json!({
            "starweaver.topic": "steering",
            "starweaver.steering_id": "late",
        })
        .as_object()
        .cloned()
        .unwrap_or_default(),
    };
    assert!(second_request.parts.contains(&expected_guard));
    assert!(second_request.parts.contains(&expected_steering));
    assert!(
        events
            .iter()
            .any(|record| matches!(record.event, AgentStreamEvent::SteeringGuard { .. }))
    );
    assert!(
        !events
            .iter()
            .any(|record| matches!(record.event, AgentStreamEvent::OutputRetry { .. }))
    );
}

#[tokio::test]
async fn output_function_result_runs_output_validators() {
    let model = Arc::new(TestModel::with_responses(vec![
        tool_call_response(
            "call_1",
            "final_answer",
            serde_json::json!({"answer": "London"}),
        ),
        tool_call_response(
            "call_2",
            "final_answer",
            serde_json::json!({"answer": "Paris"}),
        ),
    ]));

    let result = Agent::new(model.clone())
        .with_output_function(Arc::new(final_answer_function()))
        .with_output_validator(Arc::new(RequiresParis))
        .with_policy(AgentRuntimePolicy {
            max_steps: 3,
            output_retries: 1,
            ..AgentRuntimePolicy::default()
        })
        .run("answer")
        .await
        .unwrap();

    assert_eq!(result.structured_output.unwrap()["answer"], "Paris");
    assert_eq!(model.captured_messages().len(), 2);
    assert!(format!("{:?}", model.captured_messages()[1]).contains("answer must be Paris"));
}

#[tokio::test]
async fn output_function_retry_respects_retry_budget() {
    let retry_function = FunctionOutputFunction::new(
        OutputFunctionDefinition::new(
            "final_answer",
            serde_json::json!({"type": "object", "required": ["answer"]}),
        ),
        |_ctx, _args: serde_json::Value| async move {
            Err(OutputValidationError::retry("answer must be Paris"))
        },
    );
    let model = Arc::new(TestModel::with_responses(vec![tool_call_response(
        "call_1",
        "final_answer",
        serde_json::json!({"answer": "London"}),
    )]));

    let error = Agent::new(model.clone())
        .with_output_function(Arc::new(retry_function))
        .with_policy(AgentRuntimePolicy {
            max_steps: 3,
            output_retries: 0,
            ..AgentRuntimePolicy::default()
        })
        .run("answer")
        .await
        .unwrap_err();

    assert!(matches!(
        error,
        starweaver_runtime::AgentError::OutputRetryLimitExceeded { retries: 0 }
    ));
    assert_eq!(model.captured_messages().len(), 1);
}

#[tokio::test]
async fn ordinary_tool_call_still_uses_tool_loop() {
    let model = Arc::new(TestModel::with_responses(vec![
        tool_call_response("call_1", "lookup", serde_json::json!({"query": "Paris"})),
        ModelResponse::text("lookup done"),
    ]));
    let lookup = starweaver_tools::FunctionTool::new(
        "lookup",
        Some("Lookup".to_string()),
        serde_json::json!({"type": "object"}),
        |_ctx, args| async move { Ok(starweaver_tools::ToolResult::new(args)) },
    );

    let result = Agent::new(model)
        .with_output_function(Arc::new(final_answer_function()))
        .with_tools(starweaver_tools::ToolRegistry::new().with_tool(Arc::new(lookup)))
        .run("lookup")
        .await
        .unwrap();

    assert_eq!(result.output, "lookup done");
}