tinyagents 1.0.0

A recursive language-model (RLM) harness for Rust.
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
//! Tests for first-class sub-agents and recursion-depth tracking.
//!
//! These exercise:
//! - a parent harness whose scripted [`MockModel`] calls a [`SubAgentTool`],
//!   driving a child harness and composing the child's answer,
//! - direct [`SubAgent::invoke`] returning the child [`AgentRun`] at depth 1,
//! - the depth guard producing [`TinyAgentsError::SubAgentDepth`] when nested
//!   too deep (both via direct invoke and via the tool path),
//! - sub-agent lifecycle events emitted onto a shared sink.

use std::sync::Arc;

use serde_json::json;

use crate::error::TinyAgentsError;
use crate::harness::context::{RunConfig, RunContext};
use crate::harness::events::{AgentEvent, EventSink, RecordingListener};
use crate::harness::limits::RunLimits;
use crate::harness::message::{AssistantMessage, ContentBlock, Message};
use crate::harness::model::ModelResponse;
use crate::harness::providers::MockModel;
use crate::harness::runtime::{AgentHarness, RunPolicy};
use crate::harness::subagent::{SubAgent, SubAgentSession, SubAgentTool};
use crate::harness::testkit::ScriptedModel;
use crate::harness::tool::{Tool, ToolCall, ToolResult, ToolSchema};
use crate::harness::usage::Usage;

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

/// Builds a tool-call assistant response (no text, one tool call).
fn tool_call_response(id: &str, name: &str, arguments: serde_json::Value) -> ModelResponse {
    ModelResponse {
        message: AssistantMessage {
            id: Some(format!("msg-{id}")),
            content: Vec::new(),
            tool_calls: vec![ToolCall::new(id, name, arguments)],
            usage: Some(Usage::new(7, 3)),
        },
        usage: Some(Usage::new(7, 3)),
        finish_reason: Some("tool_calls".to_string()),
        raw: None,
        resolved_model: None,
    }
}

/// Builds a plain-text assistant response.
fn text_response(text: &str) -> ModelResponse {
    ModelResponse {
        message: AssistantMessage {
            id: None,
            content: vec![ContentBlock::Text(text.to_string())],
            tool_calls: Vec::new(),
            usage: Some(Usage::new(4, 2)),
        },
        usage: Some(Usage::new(4, 2)),
        finish_reason: Some("stop".to_string()),
        raw: None,
        resolved_model: None,
    }
}

/// Builds a child harness whose model always answers with `answer`.
fn child_harness(answer: &str) -> AgentHarness<()> {
    let mut harness: AgentHarness<()> = AgentHarness::new();
    harness.register_model("child-model", Arc::new(MockModel::constant(answer)));
    harness
}

/// Builds a child harness with a custom `max_depth` policy.
fn child_harness_with_max_depth(answer: &str, max_depth: usize) -> AgentHarness<()> {
    let mut harness = child_harness(answer);
    harness.with_policy(RunPolicy {
        limits: RunLimits::default().with_max_depth(max_depth),
        ..RunPolicy::default()
    });
    harness
}

struct SpinTool;

#[async_trait::async_trait]
impl Tool<()> for SpinTool {
    fn name(&self) -> &str {
        "spin"
    }

    fn description(&self) -> &str {
        "keeps the child loop running"
    }

    fn schema(&self) -> ToolSchema {
        ToolSchema::new("spin", "keeps the child loop running", json!({}))
    }

    async fn call(&self, _state: &(), call: ToolCall) -> crate::Result<ToolResult> {
        Ok(ToolResult::text(call.id, "spin", "again"))
    }
}

fn looping_child_harness_with_max_model_calls(max_model_calls: usize) -> AgentHarness<()> {
    let mut harness: AgentHarness<()> = AgentHarness::new();
    harness
        .register_model(
            "child-model",
            Arc::new(MockModel::with_tool_call("spin", json!({}))),
        )
        .register_tool(Arc::new(SpinTool))
        .with_policy(RunPolicy {
            limits: RunLimits::default().with_max_model_calls(max_model_calls),
            ..RunPolicy::default()
        });
    harness
}

// ── Tests ─────────────────────────────────────────────────────────────────

#[tokio::test]
async fn subagent_tool_drives_child_and_composes_answer() {
    let child = Arc::new(SubAgent::new(
        "researcher",
        "answers research questions",
        Arc::new(child_harness("the child answer")),
    ));
    let tool = Arc::new(SubAgentTool::new(child));

    let mut parent: AgentHarness<()> = AgentHarness::new();
    parent.register_tool(tool);
    parent.register_model(
        "parent-model",
        Arc::new(MockModel::with_responses(vec![
            tool_call_response("c1", "researcher", json!({ "input": "what is rust?" })),
            text_response("composed final answer"),
        ])),
    );

    let run = parent
        .invoke_default(&(), vec![Message::user("delegate this")])
        .await
        .expect("parent run succeeds");

    assert_eq!(run.tool_calls, 1);
    assert_eq!(run.model_calls, 2);
    assert_eq!(run.text(), Some("composed final answer".to_string()));

    // The child's answer is woven into the parent transcript as a tool message.
    let has_child_answer = run
        .messages
        .iter()
        .any(|m| matches!(m, Message::Tool(_)) && m.text() == "the child answer");
    assert!(
        has_child_answer,
        "child answer should appear as a tool result"
    );
}

#[tokio::test]
async fn direct_invoke_returns_child_run_at_depth_one() {
    let subagent = SubAgent::new(
        "helper",
        "a helper agent",
        Arc::new(child_harness("hi from child")),
    )
    .with_system_prompt("You are a helper.");

    let run = subagent
        .invoke(&(), (), 0, "hello")
        .await
        .expect("child run succeeds");

    assert_eq!(run.text(), Some("hi from child".to_string()));
    // system prompt + user input + assistant reply.
    assert_eq!(run.messages.len(), 3);
    assert!(matches!(run.messages[0], Message::System(_)));
}

#[tokio::test]
async fn invoke_at_max_depth_errors() {
    // Child harness caps depth at 1: a child run is allowed at depth 1
    // (parent_depth 0) but not at depth 2 (parent_depth 1).
    let subagent = SubAgent::new(
        "deep",
        "a deep agent",
        Arc::new(child_harness_with_max_depth("ok", 1)),
    );

    // parent_depth 0 -> child depth 1 -> within the cap.
    subagent
        .invoke(&(), (), 0, "ok")
        .await
        .expect("depth 1 within cap");

    // parent_depth 1 -> child depth 2 -> exceeds the cap of 1.
    let err = subagent
        .invoke(&(), (), 1, "too deep")
        .await
        .expect_err("depth 2 exceeds cap");
    assert!(matches!(err, TinyAgentsError::SubAgentDepth(1)));
}

#[tokio::test]
async fn tool_path_enforces_depth_limit() {
    let subagent = Arc::new(SubAgent::new(
        "deep",
        "a deep agent",
        Arc::new(child_harness_with_max_depth("ok", 1)),
    ));
    // Invoke the child at parent_depth 1 -> child depth 2 -> exceeds cap of 1.
    let tool = SubAgentTool::new(subagent).with_parent_depth(1);

    let result = tool
        .call(&(), ToolCall::new("c1", "deep", json!({ "input": "x" })))
        .await
        .expect("tool reports depth limit as a parent-visible tool result");
    assert!(result.is_error());
    assert!(
        result.content.contains("recursion depth limit")
            && result.content.contains("delegated-agent limit signal"),
        "unexpected depth limit message: {}",
        result.content
    );
}

#[tokio::test]
async fn subagent_tool_reports_child_limit_to_parent_as_tool_error() {
    let subagent = Arc::new(SubAgent::new(
        "worker",
        "does bounded work",
        Arc::new(looping_child_harness_with_max_model_calls(1)),
    ));
    let tool = SubAgentTool::new(subagent);

    let result = tool
        .call(
            &(),
            ToolCall::new("c1", "worker", json!({ "input": "loop" })),
        )
        .await
        .expect("child limit is converted into a parent-visible tool result");

    assert!(result.is_error());
    assert_eq!(result.call_id, "c1");
    assert_eq!(result.name, "worker");
    assert!(
        result.content.contains("Sub-agent `worker` stopped")
            && result.content.contains("configured run limit")
            && result.content.contains("delegated-agent limit signal"),
        "unexpected limit message: {}",
        result.content
    );
}

#[tokio::test]
async fn parent_can_continue_after_subagent_tool_hits_child_limit() {
    let subagent = Arc::new(SubAgent::new(
        "worker",
        "does bounded work",
        Arc::new(looping_child_harness_with_max_model_calls(1)),
    ));

    let mut parent: AgentHarness<()> = AgentHarness::new();
    parent
        .register_tool(Arc::new(SubAgentTool::new(subagent)))
        .register_model(
            "parent-model",
            Arc::new(MockModel::with_responses(vec![
                tool_call_response("c1", "worker", json!({ "input": "loop" })),
                text_response("worker hit its limit; I will narrow the task"),
            ])),
        );

    let run = parent
        .invoke_default(&(), vec![Message::user("delegate bounded work")])
        .await
        .expect("parent receives the sub-agent limit as a tool result and continues");

    assert_eq!(
        run.text(),
        Some("worker hit its limit; I will narrow the task".to_string())
    );
    assert_eq!(run.model_calls, 2);
    assert_eq!(run.tool_calls, 1);
    assert!(
        run.messages
            .iter()
            .any(|message| matches!(message, Message::Tool(_))
                && message.text().contains("delegated-agent limit signal")),
        "parent transcript should include the child limit tool result"
    );
}

#[tokio::test]
async fn invoke_with_events_emits_lifecycle_on_shared_sink() {
    let subagent = SubAgent::new(
        "observed",
        "an observed agent",
        Arc::new(child_harness("done")),
    );

    let sink = EventSink::new();
    let recorder = Arc::new(RecordingListener::new());
    sink.subscribe(recorder.clone());

    subagent
        .invoke_with_events(&(), (), 0, "go", &sink)
        .await
        .expect("child run succeeds");

    let events: Vec<AgentEvent> = recorder.events().into_iter().map(|r| r.event).collect();

    // Sub-agent lifecycle brackets the child run, and the child's own RunStarted
    // also lands on the shared sink.
    assert!(events.iter().any(|e| matches!(
        e,
        AgentEvent::SubAgentStarted { name, depth } if name == "observed" && *depth == 1
    )));
    assert!(events.iter().any(|e| matches!(
        e,
        AgentEvent::SubAgentCompleted { name, depth } if name == "observed" && *depth == 1
    )));
    assert!(
        events
            .iter()
            .any(|e| matches!(e, AgentEvent::RunStarted { .. }))
    );
}

#[tokio::test]
async fn invoke_in_parent_derives_unique_child_thread_ids() {
    let subagent = SubAgent::new(
        "observed",
        "an observed agent",
        Arc::new(child_harness("done")),
    );

    let sink = EventSink::new();
    let recorder = Arc::new(RecordingListener::new());
    sink.subscribe(recorder.clone());
    let parent = RunContext::new(
        RunConfig::new("parent-run").with_thread("parent-thread"),
        (),
    )
    .with_events(sink);

    subagent
        .invoke_in_parent(&(), (), &parent, "first")
        .await
        .expect("first child run succeeds");
    subagent
        .invoke_in_parent(&(), (), &parent, "second")
        .await
        .expect("second child run succeeds");

    let child_threads: Vec<String> = recorder
        .events()
        .into_iter()
        .filter_map(|record| match record.event {
            AgentEvent::RunStarted {
                thread_id: Some(thread_id),
                ..
            } => Some(thread_id.to_string()),
            _ => None,
        })
        .collect();

    assert_eq!(child_threads.len(), 2);
    assert_ne!(child_threads[0], child_threads[1]);
    for thread in child_threads {
        assert!(thread.starts_with("parent-thread-subagent-observed-d1-"));
        assert!(!thread.contains('/'));
    }
}

#[tokio::test]
async fn session_reuses_subagent_and_carries_context_across_sends() {
    // A scripted child model that records every request it receives, so we can
    // prove the SECOND send carried the FIRST turn's messages.
    let model = Arc::new(ScriptedModel::replies(vec!["Paris", "French"]));

    let mut harness: AgentHarness<()> = AgentHarness::new();
    harness.register_model("scripted", model.clone());

    let subagent = Arc::new(
        SubAgent::new(
            "geographer",
            "answers geography questions",
            Arc::new(harness),
        )
        .with_system_prompt("You are a concise geography expert."),
    );
    // Keep a separate handle to confirm the SAME Arc is reused after sends.
    let subagent_handle = Arc::clone(&subagent);

    let mut session = SubAgentSession::new(subagent);

    // First send: the user's question.
    let first = session
        .send(
            &(),
            (),
            vec![Message::user("What is the capital of France?")],
        )
        .await
        .expect("first send succeeds");
    assert_eq!(first.text(), Some("Paris".to_string()));
    assert_eq!(session.turns(), 1);

    // Second send: a follow-up human message that depends on the first answer.
    let second = session
        .send(
            &(),
            (),
            vec![Message::user("What language do they speak there?")],
        )
        .await
        .expect("second send succeeds");
    assert_eq!(second.text(), Some("French".to_string()));
    assert_eq!(session.turns(), 2);

    // The SAME underlying SubAgent (and harness) was reused — never rebuilt.
    assert!(
        Arc::ptr_eq(session.subagent(), &subagent_handle),
        "the session must reuse the same SubAgent Arc across sends"
    );

    // The scripted model saw exactly two requests (one per send) — a single,
    // reused harness instance accumulated both.
    let requests = model.requests();
    assert_eq!(
        requests.len(),
        2,
        "one model request per send on one harness"
    );

    // The SECOND request CONTAINED the first turn's messages (context carried):
    // the original question, the first assistant answer, and the system prompt.
    let second_texts: Vec<String> = requests[1].messages.iter().map(Message::text).collect();
    assert!(
        second_texts.contains(&"What is the capital of France?".to_string()),
        "second request should retain the first user question, got {second_texts:?}"
    );
    assert!(
        second_texts.contains(&"Paris".to_string()),
        "second request should retain the first assistant answer, got {second_texts:?}"
    );
    assert!(
        second_texts.contains(&"What language do they speak there?".to_string()),
        "second request should include the follow-up human message, got {second_texts:?}"
    );
    // The fixed system prompt is seeded exactly once (not duplicated per send).
    let system_count = requests[1]
        .messages
        .iter()
        .filter(|m| matches!(m, Message::System(_)))
        .count();
    assert_eq!(system_count, 1, "system prompt seeded once, not per send");
}

#[tokio::test]
async fn session_emits_reuse_event_only_after_first_send() {
    let mut harness: AgentHarness<()> = AgentHarness::new();
    harness.register_model("scripted", Arc::new(ScriptedModel::replies(vec!["a", "b"])));
    let subagent = Arc::new(SubAgent::new("helper", "helps", Arc::new(harness)));

    let sink = EventSink::new();
    let recorder = Arc::new(RecordingListener::new());
    sink.subscribe(recorder.clone());

    let mut session = SubAgentSession::new(subagent).with_events(sink);

    session
        .send(&(), (), vec![Message::user("one")])
        .await
        .expect("first send");
    session
        .send(&(), (), vec![Message::user("two")])
        .await
        .expect("second send");

    let events: Vec<AgentEvent> = recorder.events().into_iter().map(|r| r.event).collect();
    let reused: Vec<usize> = events
        .iter()
        .filter_map(|e| match e {
            AgentEvent::SubAgentReused { name, turn } if name == "helper" => Some(*turn),
            _ => None,
        })
        .collect();
    assert_eq!(
        reused,
        vec![1],
        "exactly one reuse event, for the second send (turn 1)"
    );
    // Both sends still emit the started/completed bracket (depth 1).
    let started = events
        .iter()
        .filter(|e| matches!(e, AgentEvent::SubAgentStarted { depth, .. } if *depth == 1))
        .count();
    assert_eq!(started, 2, "each send brackets with SubAgentStarted");
}

#[tokio::test]
async fn session_reset_clears_transcript_and_turns() {
    let mut harness: AgentHarness<()> = AgentHarness::new();
    harness.register_model("scripted", Arc::new(ScriptedModel::replies(vec!["x", "y"])));
    let subagent = Arc::new(SubAgent::new("r", "resets", Arc::new(harness)));

    let mut session = SubAgentSession::new(subagent);
    session
        .send(&(), (), vec![Message::user("first")])
        .await
        .expect("send");
    assert_eq!(session.turns(), 1);
    assert!(!session.transcript().is_empty());

    session.reset();
    assert_eq!(session.turns(), 0);
    assert!(session.transcript().is_empty());
}

#[test]
fn subagent_tool_schema_uses_name_and_description() {
    let subagent = Arc::new(SubAgent::new(
        "writer",
        "writes prose",
        Arc::new(child_harness("x")),
    ));
    let tool = SubAgentTool::new(subagent);
    let schema = tool.schema();
    assert_eq!(schema.name, "writer");
    assert_eq!(schema.description, "writes prose");
    assert_eq!(schema.parameters["required"][0], "input");
}